综合编程

Placeholder segments in express middleware mount points

微信扫一扫,分享到朋友圈

Placeholder segments in express middleware mount points
0 0

When using Express I can define routes with a placeholder in the route string, something like:

app.get("/users/:user_id/photos", function(req,res){<blah>});

and then in my handler I can user req.params["user_id"]
to get whatever was in the URL and use it in my request.

Middleware can be mounted at certain paths such that only requests matching that path will use the middleware. Can I use placeholders in the mount path of a middleware? For example, could I do something like:

app.use("/users/:user_id/photos", <middleware>);

and then inside the middleware have some way of accessing what the segment that maps to :user_id
was?

EDIT 1:

I am aware that I can put the middleware directly in the route declaration, à la:

app.get("/users/:user_id/photos", <middleware>, function(req,res){<blah>});

It doesn't take much imagination to see how that would get out of hand as an app grows.

Problem courtesy of: juanpaco

Node.js Recipes
感谢您的支持!

    This is the Galaxy A8s, complete with display hole camera and triple camera setup

    上一篇

    MySQL 8 and The FRM Drop… How To Recover Table DDL

    下一篇

    您也可能喜欢

    评论已经被关闭。

    插入图片

    热门分类