在重定向之前设置快速响应 headers
Set Express response headers before redirect
我正在实施一个站点登录,该登录接受 email/password 组合,检索 API 令牌,然后 returns 将其发送给用户以将其存储(加密)在 localStorage 中.
目前,在成功发布到 /login
后,该应用会将用户重定向到索引页面,并附加令牌作为查询,如下所示(如建议 here):
login.post('/', function(req, res) {
...checking password...
Auth.getToken(user, function(err, token) {
res.redirect('/?token=' + token);
});
});
这很好用,但我希望尽可能保持我的 URL 干净,并将令牌设置为 header:
login.post('/', function(req, res) {
...checking password...
Auth.getToken(user, function(err, token) {
res.set('x-access-token', token);
console.log(res._headers);
// --> {'x-powered-by': 'Express', 'x-access-token': <token>}
res.redirect('/');
});
});
console.log
-ing res._headers
显示 header 已按预期设置,但是当我在索引页面的请求上记录 req.headers
时,它不是出现:
{ host: 'localhost:3000',
connection: 'keep-alive',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
referer: 'http://localhost:3000/login',
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'en-US,en;q=0.8',
cookie: 'ifusr=crwj; _ga=GA1.1.1933420201.1409901705',
'if-none-match': '"1195161647"' }
感谢任何建议!
设置headers在这里不起作用,因为重定向将执行一个新的http请求,您可以使用express-session来存储授权令牌并在需要时获取它
req.session.accessToken = token
我正在实施一个站点登录,该登录接受 email/password 组合,检索 API 令牌,然后 returns 将其发送给用户以将其存储(加密)在 localStorage 中.
目前,在成功发布到 /login
后,该应用会将用户重定向到索引页面,并附加令牌作为查询,如下所示(如建议 here):
login.post('/', function(req, res) {
...checking password...
Auth.getToken(user, function(err, token) {
res.redirect('/?token=' + token);
});
});
这很好用,但我希望尽可能保持我的 URL 干净,并将令牌设置为 header:
login.post('/', function(req, res) {
...checking password...
Auth.getToken(user, function(err, token) {
res.set('x-access-token', token);
console.log(res._headers);
// --> {'x-powered-by': 'Express', 'x-access-token': <token>}
res.redirect('/');
});
});
console.log
-ing res._headers
显示 header 已按预期设置,但是当我在索引页面的请求上记录 req.headers
时,它不是出现:
{ host: 'localhost:3000',
connection: 'keep-alive',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
referer: 'http://localhost:3000/login',
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'en-US,en;q=0.8',
cookie: 'ifusr=crwj; _ga=GA1.1.1933420201.1409901705',
'if-none-match': '"1195161647"' }
感谢任何建议!
设置headers在这里不起作用,因为重定向将执行一个新的http请求,您可以使用express-session来存储授权令牌并在需要时获取它
req.session.accessToken = token