我有一个带有一些 API 路由的 Express 服务器,如下所示:
server.post("/api/send-email", (req, res) => {
});
});
Run Code Online (Sandbox Code Playgroud)
您不需要身份验证令牌即可访问 API,但我只希望我的网站mydomain.com能够使用它。我尝试过启用限制访问,如下所示:
function restrictAccess(req, res, next) {
if (req.headers['origin'] !== 'http://localhost:3000') {
res.sendStatus(403);
} else {
next();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将限制访问作为中间件传递到我的路由中。当我使用邮递员发出 POST 请求时,我无法再使用 API,但我只需更改原始标头即可再次访问它。
我怎样才能只允许来自 的请求mydomain.com?我已经在互联网上搜索了很长时间,但找不到任何东西。有可能吗?
我想用另一个字符串替换 Dart 中的 URL 字符串。例子:
if (url == "http://www.example.com/1") {
home = "example";
} else if (url == "http://www.example.com/2") {
home = "another example";
}
Run Code Online (Sandbox Code Playgroud)
难道没有更好的方法,代码更少,速度更快吗?我必须这样做超过 60 次..