Generate expressjs routes from an array of strings?
Is it possible to generate a list of routes from an array of strings?
For example, I have this list:
const routes = ["/test","/test2","/test3"];
and I want to generate these routes:
const Routes = app => {
app.post("/test", apiCall);
app.post("/test2", apiCall);
app.post("/test3", apiCall);
};
export default Routes;
Then Routes
is called in a different file like this:
Routes(app);
Solution 1:
If you want the same apiCall
for all these routes, you can simply write
app.post(["/test","/test2","/test3"], apiCall);
as explained in express documentation. So no need to generate anything.