Manish Kumar Manish Kumar - 10 months ago 69
Node.js Question

Redirect to different page url in Node.js (Not in express or other frameworks)

I want to redirect user from one page to another page in Node.js (plain node.js)

Real life scenario: Afer signup (, after successful signup I want to redirect users to login page(

if (signUpSuccessful(request, response)) {
// I want to redirect to "/login" using request / response parameters.

Answer Source

It's simple:

if (signUpSuccessful(request, response)) {
    response.statusCode = 302; 
    response.setHeader("Location", "/login");

This will redirect your user to the /login URL with an 302 Found status and finish the response. Be sure that you haven't called response.write() before, otherwise an exception will be thrown.