简体   繁体   English

覆盖默认的快速路由URL处理程序

[英]Overriding default express route URL handler

The default express router uses : (colon) to mark a parameter eg 默认的快速路由器使用:冒号)标记参数,例如

users/:id

But when I was using other (non node.js) framework, I have noticed that they use curly brackets, eg 但是当我使用其他(非node.js)框架时,我注意到它们使用大括号,例如

users/{id}

And I prefer the second style because browser escapes curly brackets unlike colon (Which I want to use in my URL), and, I just like it. 我喜欢第二种样式,因为浏览器不像冒号(我想在我的URL中使用)转义大括号,而且,我喜欢它。

So, the question is, can I override default express route URL handler? 因此,问题是,我可以覆盖默认的快递路线URL处理程序吗?

You don't need to include the colon in the url. 您无需在网址中包含冒号。

If you use users/:id 如果使用users/:id

And get this url 并获取此网址

users/something 用户/东西

Here your req.params will be: 这里您的req.params将是:

{
   id:"something"
}

Unfortunately you cant. 不幸的是你不能。 The Express.js framework relies on path-to-regexp library for extracting params from urls. Express.js框架依赖于正则表达式路径库从URL中提取参数。 And its usage is hardcoded inside the codebase , so you don't have any extension points to override that behavior. 而且它的用法已在代码库中进行了硬编码,因此您没有任何扩展点可以覆盖该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM