我的前端运行在react + flux + node和后端运行在Java中。 我正在使用superagent将响应操作的请求发送到成功发生的后端,我正在尝试从后端发送自定义标头,这也正常工作并从后端发送到前端,我可以在浏览器网络中看到(调试请求和响应)。但是当我尝试访问反应动作中的标题时,我没有看到它。有人请指导我如何处理这个?
我想要访问的自定义标头是X-Token但是我无法在反应动作的superagent的响应中看到它。 反应动作功能如下所示:
login(payload) {
request.post(someurl+'/login')
.send({
username: payload.userEmail,
password: payload.userPassword
})
.set('Accept', 'application/json')
.end(function(err, res){
if (err || !res.ok) {
} else {
console.log(res.headers) //it doesnt give x-token header.
}
});
}
}
以下是我的服务器js内容:
var app = express();
app.options('*', cors());
app.set('port', process.env.PORT || 3001);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader("Access-Control-Allow-Headers", "X-Token,Origin,X-Requested-With,content-type");
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(express.static(path.join(__dirname, 'public')));
app.engine('html', consolidate['swig']);
app.set('view engine', 'html');
app.set('views', './views');
app.use(function(req, res) {
Router.match({ routes: routes.default, location: req.url }, function(err, redirectLocation, renderProps) {
if (err) {
res.status(500).send(err.message)
} else if (redirectLocation) {
res.status(302).redirect(redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
var html = ReactDOM.renderToString(React.createElement(Router.RoutingContext, renderProps));
var page = swig.renderFile('views/index.html', { html: html });
res.status(200).send(page);
} else {
res.status(404).send('Page Not Found')
}
});
});
require('./app/routes/server-route')(app, express);
var server = require('http').createServer(app);
server.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
提前感谢您的帮助。