本文介绍了如何在 express js 中执行 res.redirect 时传递标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理 express js,我需要重定向到需要身份验证的页面.这是我的代码:
I am working on express js and I need to redirect to a page which needs authentication. This is my code:
router.get('/ren', function(req, res) {
var username = 'nik',
password = 'abc123',
auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
res.redirect('http://localhost:3000/api/oauth2/authorize');
})
如何为这个重定向命令设置标题?
How can I set headers to this redirect command?
推荐答案
如果使用 301 (Moved Permanently) 或 302 (Found) 重定向,是否不会自动转发标头?
Doesn't express forward the headers automatically if you redirect with 301 (Moved Permanently) or 302 (Found)?
如果没有,您可以通过以下方式设置标题:
If not, this is how you can set headers:
res.set({
'Authorization': auth
})
或
res.header('Authorization', auth)
然后调用
res.redirect('http://localhost:3000/api/oauth2/authorize');
最后,这样的事情应该可行:
Finally, something like that should work:
router.get('/ren', function(req, res) {
var username = 'nik',
password = 'abc123',
auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
res.header('Authorization', auth);
res.redirect('http://localhost:3000/api/oauth2/authorize');
});
这篇关于如何在 express js 中执行 res.redirect 时传递标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!