Ali*_*eki 0 api rest node.js express
我有一个 MEAN 堆栈应用程序并使用 Node.js 和 Express.js 作为后端 API。
假设我有一个“评论”路线如下
/* GET /comments listing. */
router.get("/", function(req, res, next) {
Comment.find(function(err, comments) {
if (err) return next(err);
res.json(comments);
});
});
Run Code Online (Sandbox Code Playgroud)
并在我的服务器中像这样使用它:
var commentsRouter = require('./routes/comments');
...
app.use('/comments', commentsRouter);
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法阻止用户http://mrUrl/comments在浏览器中访问并拒绝可能带有 403 Forbidden 消息的请求,但同时 JavaScript 文件尝试访问相同的 URL 将收到一条内容消息(在示例中应该是res.json(comments);)
此外,是否可以对所有路由启用一次此类限制,而不是对每个路由启用此类限制。
是的,您可以使用middleware.
中间件是您可以在正在执行的主函数之前或之后传递的函数(在本例中为GET comments)
函数位置的顺序很重要,首先是什么 - 首先执行,然后像这样实现它:
app.use(myBrowsingRestrictionMiddlewareFunction) // Runs
app.use('/comments', commentsRouter);
app.use('/account', accountRouter);
Run Code Online (Sandbox Code Playgroud)
您还可以在route handler:
app.post('/comments', myMakeSureDataIsAlrightFunction, myMainCreateCommentFunction, myAfterStatusWasSentToClientAndIWishToMakeAnotherInternalActionMiddleware);
Run Code Online (Sandbox Code Playgroud)
属性req, res, next会自动传递到函数中。
这意味着,myBrowsingRestrictionMiddlewareFunction接收它们,您可以像这样使用它们:
export function myBrowsingRestrictionMiddlewareFunction(req, res, next) {
if (req.headers['my-special-header']) {
// custom header exists, then call next() to pass to the next function
next();
} else {
res.sendStatus(403);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
扩展关于在 FS 结构中放置中间件的位置(个人建议):
我喜欢做的是将路由器与 app.js 分开,如下所示:
应用程序.js
app.use('/', mainRouter);
Run Code Online (Sandbox Code Playgroud)
路由器.js
const router = express.Router();
router.use(middlewareForAllRoutes);
router.use('/comments', commentsRouter);
router.use(middlewareForOnlyAnyRouteBelow);
router.use('/account', accountRouter);
router.use(middlewareThatWillBeFiredLast); // To activate this, remember to call next(); on the last function handler in your route.
Run Code Online (Sandbox Code Playgroud)
评论Router.js
const router = express.Router();
router.use(middlewareForAllRoutesONLYFORWithinAccountRoute);
route.get('/', middlewareOnlyForGETAccountRoute, getAccountFunction);
router.post('/', createAccount);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4510 次 |
| 最近记录: |