node.js - Express.js Find URL of client consuming REST API
问题描述
Lets say, I have a server running at: https://example.com
The code in server:
const express = require("express");
const app = express();
app.get('/',(req, res)=>{
let data = {'Hello':'World'}
res.json(data);
});
app.post('/',(req, res)=>{
let name = req.body.name;
let email = req.body.email;
res.json({name, email});
});
app.listen(3000);
Now, there is a client: https://website.com
, trying to access server response by making a GET and POST request. (No API key is required)
How can the server find the web address of the client?
In this example, I want the server (example.com)
to determine the client's URL (website.com)
and save it in the database. req.hostname()
is not giving the desired output..
解决方案
req.hostname是您要查找的内容。
更新:
重新阅读您的问题后,我认为您想要的是识别跨域请求,您将改为使用Origin
标题。
var origin = req.get('origin');
请注意,一些跨域请求需要通过“预检”请求进行验证:
req.options('/route', function (req, res) {
var origin = req.get('origin');
// ...
});
推荐阅读
- odata - 可以使用 SAP oData 导航属性将第一个实体的键获取到相关实体的数据提供程序类中吗?
- docker - Docker - Windows10 WSL2 - 编译速度“超级糟糕”
- batch-file - 远程启动批处理脚本,本地交互
- nativescript - 在 Svelte Native 中显示来自 fetch 的列表视图
- java - 用于从项目中获取方法的调用层次结构的 Java API
- wordpress - Wordpress get_posts() 仅显示用户个人资料中自定义帖子的部分正确数据
- excel - 跨工作簿 VBA 的 VLookup
- node.js - s3.listObjectsV2 不是 NodeJs 的函数
- monitoring - Prometheus 中的最小 scrape_interval 是多少?
- python - 如何在python中打印“用户导入”模块名称和版本