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

前端 未结 3 973
[愿得一人]
[愿得一人] 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:05

    Simplest way to do it is by using a 302 status code and a location field with the target URL.

    The HTTP response status code 302 Found is a common way of performing a redirection.

    An HTTP response with this status code will additionally provide a URL in the Location header field. The User Agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request, to the new URL specified in the Location field. The HTTP/1.0 specification (RFC 1945) defines this code, and gives it the description phrase "Moved Temporarily".

    Source: Wikipedia

    res.statusCode = 302;
    res.setHeader("Location", '/login');
    res.end();
    

提交回复
热议问题