求问expressjs里的res.location和res.redirect的区别
发布于 9 年前 作者 klausgao 4652 次浏览 最后一次编辑是 8 年前 来自 问答

感觉用哪个都不确定,都是要试试,不然总会有各种问题,比如redirect经常会500,但是却能正常运行下去。

3 回复

经常会302 Error: Can’t set headers after they are sent.

res.locationres.redirect 操作步骤的其中之一。

如果分不清他们的区别,一般你是需要 res.redirect

res.location 只是单纯的设置 header 头部的 location: 字段,设置完之后就没事了。也不会帮忙调用 res.end() res.redirect 会设置 location: 字段,然后把状态吗设置成 302,并且直接调用 res.end() 进行返回。

@alsotang 谢谢大神!

回到顶部