user3025403
user3025403

Reputation: 1080

Expressjs Route from Variable

I have an array:

var arr = ["/index.html", "/alternative_index.html", "/index"]

and I want the Express server to return the same thing for all these routes:

localhost:8080/index.html
localhost:8080/alternative_index.html
localhost:8080/index

This works:

app.get("/index.html|/alternative_index.html|/index", (req, res) => {
    console.log("Here")
    ...
}

So I defined a variable that is the same as the route above:

// returns "/index.html|/alternative_index.html|/index"
var indexRoutes = arr.join("|")

However, this does not work:

app.get(indexRoutes, (req, res) => {
    console.log("Here")
    ...
}

I also tried using a RegExp for indexRoutes and that also did not work.

Why is Express not registering the right route when I define it using a variable?

Upvotes: 1

Views: 53

Answers (1)

David Espino
David Espino

Reputation: 2187

Have you tried passing the array directly? app.get(['url1', 'url2', 'url3'], (req, res) => { console.log('here'); })

Regards

Upvotes: 1

Related Questions