helpisgood
helpisgood

Reputation: 381

Changing URL in NodeJS only

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)?

Upvotes: 1

Views: 2318

Answers (1)

shukshin.ivan
shukshin.ivan

Reputation: 11340

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();

Upvotes: 1

Related Questions