Javascript Express js授权值未定义
我打开了两个端口:3000和4000。我想从3000传递授权头并在端口4000接收它 我正在从3000端口传递授权值Javascript Express js授权值未定义,javascript,node.js,Javascript,Node.js,我打开了两个端口:3000和4000。我想从3000传递授权头并在端口4000接收它 我正在从3000端口传递授权值 app.get('/',function(req,res){ console.log('redirect'); res.header('Authorization', 'my sample'); res.redirect('http://localhost:4000/api/oauth2'); }); 在4000端口接收 app.get('/api/oauth2',
app.get('/',function(req,res){
console.log('redirect');
res.header('Authorization', 'my sample');
res.redirect('http://localhost:4000/api/oauth2');
});
在4000端口接收
app.get('/api/oauth2', (req, res)=> {
console.log(req.headers.authorization); // undefined
res.end('i reached');
})
如何从3000到4000端口接收该值?您必须使用npm的请求包,npm是一个http客户端
npm install request
导入请求模块
const request = require('request');
let options = {
url: "http://localhost:4000/api/oauth",
headers:{
authorization: "your_authorization_token"
}
request.get(options, (err, response, body)=>{
// Handle response
})
您可以使用在客户端发出请求的.html文件返回“/”请求,也可以在url查询中设置身份验证令牌。您可以设置
授权
并尝试访问授权
。我认为headers键是区分大小写的。我更改了,但它不起作用。您可能需要记录req.headers
并查看它有什么功能。req
对象的'//code>与'/api/oauth2'
对象隔离,因为您使用了重定向方法(“嘿,浏览器-客户端让我们去http://..."-所述方法)。尝试使用url查询字符串,而不是将数据设置为标题。标题只是请求或响应的信息,比如内容类型
…它从端口3000发送数据,对吗?在4000端口我将如何接收