如何在express js中执行res.redirect时传递标头 [英] How to pass headers while doing res.redirect in express js

查看:386
本文介绍了如何在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(永久移动)或302(找到)重定向​​,会自动标题?

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时传递标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆