重定向到Node.js中的不同页面URL(不在快速或其他框架中)

Man*_*mar 8 node.js

我想将用户从一个页面重定向到Node.js中的另一个页面(plain node.js)

现实生活场景:注册成功后(example.com/sigup),注册成功后我想将用户重定向到登录页面(example.com/login).

if (signUpSuccessful(request, response)) {    
    // I want to redirect to "/login" using request / response parameters.
}
Run Code Online (Sandbox Code Playgroud)

gus*_*nke 11

这很简单:

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

这会将您的用户重定向到/login具有302 Found状态的URL 并完成响应.请确保您之前没有调用response.write()过,否则将引发异常.