How would I mass create routes or efficiently make thousands of routes with express?
I would like to have routes that are of the format - localhost:3000/${insert address here}
Do I make a for loop or is there some feature that express has built in? For example, yelp has "https://www.yelp.com/biz/mcdonalds-san-jose-19." I can't seem to find any resources about this.
For thousands of routes that all have a similar form such as your example of:
https://www.yelp.com/biz/mcdonalds-san-jose-19
You create one route with a parameter and then that one route handler looks up the parameter (in a database or a some lookup table in your code) to get a pointer to the appropriate content for that tag.
You would implement a route like this with a parameter such as:
app.get("/biz/:location", (req, res) => {
const location = req.params.location;
// now get the appropriate content for that location using some
// form of lookup and send the response
});
Then, you have one route that serves zillions of possible locations and there is no need to register zillions of different routes, one for each location.
If you weren't familiar with URL parameters in Express routes, the :location
is a placeholder for some string that fits in the pattern there and a URL that matches this pattern will match this route and the route handler can access that parameter at req.params.location
where the location
property name comes from the name used in the URL pattern. More details is explained in the Express doc here.