Response redirect in Connect

霸气de小男生 提交于 2021-01-29 14:35:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!