1

I have the following segment of code in my server callback:

switch (request.url) {
    case "/somescript" :
        response.writeHead(200, {"Content-Type": "text/javascript"});
        response.write(somescript);
        break;      
    default :
        response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"});
        response.write(html);
}
response.end();

When I run the server, when I enter this:

localhost:3000

on the browser, how can I have it change automatically to:

localhost:3000/changed

Only using Node.js (in my default section of the switch statement)?

1 Answer 1

1

Just add response.writeHead(301, {"Location": "/changed"}); to default.

switch (request.url) {
    case "/somescript" :
        response.writeHead(200, {"Content-Type": "text/javascript"});
        response.write(somescript);
        break;      
    case "/changed" :
        response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"});
        response.write(changedhtml);
        break;
    default :
        response.writeHead(301, {"Location": "/changed"});`
}
response.end();
Sign up to request clarification or add additional context in comments.

3 Comments

Then I get the error "The localhost page isn’t working, localhost redirected you too many times."
Well, you have to add case "/changed": too to prevent it.
I tried that initially and got a different error: The webpage at localhost:3000/changed might be temporarily down or it may have moved permanently to a new web address. ERR_INVALID_CHUNKED_ENCODING

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.