问题
In Express, Im able to redirect to other url using response.redirect(""). Similarly how can I redirect in Connect module? I've tried the below code but its not working.
response.setHeader('Content-Type', 'text/plain');
response.end('<p>302. Redirecting to <a href="' + url+ '">' + url+ '</a></p>');
回答1:
You can also redirect in Connect using writeHead
as follows:
res.writeHead(301, {Location: url});
res.end();
The 301 http status code means "moved permanently".
回答2:
res.redirect
is defined in express, not in connect (see the relevant source code). You then can't use this function, but you can copy its behavior by setting the Location
header:
res.set('Location', url);
You can also read this answer: even if is related to php, it contains useful information regarding the use of the header.
来源:https://stackoverflow.com/questions/22141137/response-redirect-in-connect