Placeholder segments in express middleware mount points

综合编程 2018-12-07 阅读原文

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

责编内容by:Node.js Recipes阅读原文】。感谢您的支持!

您可能感兴趣的

Accessing JSON values in an express web server I was able to use the code in this answer to access a value in a JSON string posted to the server. If the...
Node.js + Express + Jade :: render( … ) time... I've been using Node.js + Express + Jade for a long time. Since upgrading to node 0.8.12, the render( ... ) comman...
django的登录注册系统 摘要 django框架本身自带有登录注册,也可以自己写登录注册,下面将介绍这这2种方式实登录注册 一、自己写登录注册登出 1.注册regist 注册采用的是form表单,提交到数据库,在登录的时候,查询数据看,看用户有...
Koa 源码浅析 本文围绕koa服务从启动,到处理请求再到回复响应这个过程对源码进行简单的解析 在koa中ctx是贯穿整个请求过程的,它是这次请求原信息的承载体,可以从ctx上获取到request、response、cookie等,方便我们进行后续的计...
一次测试引发的探索:关于分号在javaweb中的一个特性... *本文原创作者:wadcl,本文属FreeBuf原创奖励计划,未经许可禁止转载 还记得IIS的解析漏洞吗?xx.asp;.jpg。而最近对java网站进行测试的时候发现了一个问题,也是由分号引起的,也因此去查看了一下源码...