Javascript 从node.js中的请求获取客户端域&;express.js
我想在后端获得客户端的主机域。 e、 gJavascript 从node.js中的请求获取客户端域&;express.js,javascript,node.js,express,Javascript,Node.js,Express,我想在后端获得客户端的主机域。 e、 g 前端() 后端() 在前端,使用ajax将api requeset发送到后端 $.get('http://localhost:4000/auth'); 在后端方面,我喜欢这样做 // routes/auth.js router.get('/', (req, res) => { console.log(req.headers.referer); ... }); ... module.exports = router; 这是我在后端的ap
- 前端()
- 后端()
$.get('http://localhost:4000/auth');
在后端方面,我喜欢这样做
// routes/auth.js
router.get('/', (req, res) => {
console.log(req.headers.referer);
...
});
...
module.exports = router;
这是我在后端的app.js
const express = require('express');
const app = express();
const authRoutes = require('./routes/auth.js');
...
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use('/auth', authRoutes);
...
预期结果:
localhost:3000
当前结果:
undefined
它显示后端url
当我执行console.log(req.headers)时,它如下所示
- 邮递员的要求
- 前端请求
http://localhost:4000
指的是http://localhost:5000
,然后将设置参考值
或者,referer
不是您真正想要的。看看。@Subburaj是的,主机也是“localhost:4000”,我添加了req.headerstry:console.log(req.headers.host);
{ 'user-agent': 'PostmanRuntime/7.17.1',
accept: '*/*',
'cache-control': 'no-cache',
'postman-token': '5eb5791e-3a5a-4285-83c9-33320d935a2e',
host: 'localhost:4000',
'accept-encoding': 'gzip, deflate',
cookie:
'connect.sid=s%3A5DIRzqn5HVL3vra410YJ6I56uo9qIj2M.6OWL8rKr1peEMz60sakejaeJJNgv5LQFUKypA6cCXLQ',
connection: 'keep-alive' }
{ host: 'localhost:4000',
accept: '*/*',
'content-type': 'application/json' }