在我的MEAN应用中,我有一个API和静态Web文件,例如index.html和login.html。
//Routes
app.use('/mysuperapi', require('./routes/api'));
//For static files
app.use(express.static('site'));
Run Code Online (Sandbox Code Playgroud)
如果我不希望用户能够mysite.com/mysuperapi自由访问URL,例如,我想知道是否有可能?
login.html在用户机器中会出现在API中调用身份验证路由的页面,因此我无法将所有期望服务器使用的IP完全列入白名单。
还是所有API都是公开的?
任何建议表示赞赏。
谢谢。
您可以通过添加另一个中间件层来限制对路由的访问,例如
app.use('/mysuperapi/*', function(req, res, next) {
// do your filtering here, call a `res` method if you want to stop progress or call `next` to proceed
var ip = req.ip ||
req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
// Your allowed ip. You can also check against an array
if (ip == '5.4.3.2') {
next();
} else {
res.end();
}
}
app.use('/mysuperapi', require('./routes/api'));
// ... the rest of your code
Run Code Online (Sandbox Code Playgroud)
在与模式匹配的所有路径以及到达API之前,都会调用第一个app.use语句上的函数,因此您可以在此处限制访问。
| 归档时间: |
|
| 查看次数: |
2091 次 |
| 最近记录: |