[英]set custom headers dynamically in express app to be accessible by angular app
i am quiet new to express .
我很安静。 I have searched a lot and unable to find the answer.
我已经搜索了很多,却找不到答案。 i am receiving headers in my express app, but when i try to set those headers to be accessible by angular, it returns undefined.
我在我的Express应用程序中接收标头,但是当我尝试将这些标头设置为可通过angular访问时,它返回未定义。 how ever hard coded header appears perfectly at angular end in networks tab of chrome.
硬编码标头如何完美地出现在chrome的网络标签的角端。 Here is the code
这是代码
express code
快递代码
var express = require('express');
var config = require('./config')[process.env.NODE_ENV || 'dev'];
var app = express();
app.get('/*', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
var token=req.header('x-access-token');
//var token='123'
console.log(token);
res.set('x-access-token',token);
});
var server = app.listen(process.env.PORT || 3000, function() {
console.log('server started. Listening on port '+3000);
});
Angular code
角度代码
$http.get($scope.windowURL
).
success(function(data, status, headers, config) {
$scope.token=headers('x-access-token')
console.log($scope.token);
alert($scope.token)
})
.error(function(data, status, headers, config) {
alert('notoken')
});
and i am getting 'undefined' in headers if set dynamically by express app. 如果快递应用程序动态设置标题,则会在标题中显示“未定义”。
You should set the header, before finishing the response with res.sendFile(); 您应该先设置标头,然后再使用res.sendFile()完成响应。
var express = require('express');
var config = require('./config')[process.env.NODE_ENV || 'dev'];
var app = express();
app.get('/*', function (req, res) {
var token=req.header('x-access-token');
//var token='123'
console.log(token);
res.set('x-access-token',token);
res.sendFile(__dirname + '/public/index.html');
});
var server = app.listen(process.env.PORT || 3000, function() {
console.log('server started. Listening on port '+3000);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.