Expressjs从variables路线
我有一个数组:
var arr = ["/index.html", "/alternative_index.html", "/index"]
我希望Express服务器为所有这些路由返回相同的内容:
localhost:8080/index.html localhost:8080/alternative_index.html localhost:8080/index
这工作:
app.get("/index.html|/alternative_index.html|/index", (req, res) => { console.log("Here") ... }
所以我定义了一个和上面的路线相同的variables:
// returns "/index.html|/alternative_index.html|/index" var indexRoutes = arr.join("|")
但是,这不起作用:
app.get(indexRoutes, (req, res) => { console.log("Here") ... }
我也尝试使用RegExp
indexRoutes
,也没有工作。
为什么Express在我使用variables定义时没有注册正确的路线?
你有没有尝试直接传递数组? app.get(['url1', 'url2', 'url3'], (req, res) => { console.log('here'); })
问候