How Do I Create A Temporary Endpoint In Express

1997 views javascript
4

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.

answered question

Do you want it to be used once and just fail the next time?

1 Answer

0

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.

posted this

Have an answer?

JD

Please login first before posting an answer.