Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of express URL parameterized routing

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to solve the problem of express URL parameterized routing". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Background

In the recent development process, we found that some colleagues put the express routing location improperly, which caused our own middleware to report an error when verifying URL Parameter, and shared a tip to solve parameterized routing through card data sharing.

two。 Inappropriate route definition r.route ('PUT',' / bind') r.route ('PUT',' / unbind') r.route ('PUT',' /: deviceId')

In the above route, the middleware request object of express will think that bind and unbind are also the values of the parameter deviceId. We have a middleware that just restricts all URL Parameter to be of number type, which causes an deviceId type error to be reported when requesting / bind and / unbind. It should be noted that this error does not affect the use of the correct route. But the backend will throw an exception is also an unacceptable thing, all need to find a solution. Because the Parameter is obtained through the express standard interface, it is not appropriate to check the type of compatible lines in the middle tier. We should find a way to let express know that our bind and unbind are not variables.

3. Solution

Set more accurate routing rules to allow express to discover URL parameters more accurately, so the third route definition can be modified as follows:

R.route ('PUT',' /: deviceId (\\ d +)')

With the addition of (\ + d), route matching helps filter values such as bind and unbind, the standard interface does not return illegal them, and the middleware does not need to be modified.

This is the end of the content of "how to solve the express URL parameterized routing". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report