Dev01
Dev01

Reputation: 14169

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);

Upvotes: 0

Views: 45

Answers (1)

Heiko Theißen
Heiko Theißen

Reputation: 17507

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.

Upvotes: 1

Related Questions