简体   繁体   中英

How Do I Create A Temporary Endpoint In Express

I'm using the node.js module express, and I want to create a temporary endpoint. Either one that is removed once it is visited once, or one that I can manually remove. Thanks in advance.

You would typically create some type of wildcard route that matches all of the temporary endpoints. Then, inside the route handler, you would check if this is still a valid temporary endpoint or not (by checking some data structure in memory or in a database).

Then you aren't dynamically creating routes and removing routes. Instead, you make one master route and use logic inside the route handler to decide if this particular temporary route is still valid or not?

Here's an example:

// single route handler for all temporary routes with one common prefix
app.get('/show/:id', (req, res) => {
   // dynamically check req.params.id to see if it's still a valid temporary route or not
   // by looking it up in some data structure (either in memory or in a database)
   // if it's valid, serve the content
   // if not valid return a 404 or some other appropriate error status
});

This is also a lot more scalable to millions of temporary endpoints and to clustered servers without burdening the routing system of the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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