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

前端 未结 3 972
[愿得一人]
[愿得一人] 2021-01-12 05:30

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

Real life scenario: Afer signup (example.com/sigup), after successful signup I want

3条回答
  •  别那么骄傲
    2021-01-12 06:06

    It's simple:

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

    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.

提交回复
热议问题