我在NodeJs项目中使用了express,我希望能够使用以下路由请求我的服务器:
/dogs
/pinguin
/bear
/wolf
/cat
/rat
我对此使用正则表达式(http://forbeslindesay.github.io/express-route-tester/):快速路线测试仪
它与快速路由测试程序一起正常工作,但当我尝试使用NodeJS 时失败了
我的代码:
var express = require('express');
var app = express()
app.get("/(dogs|pinguin|bear|wolf|cat|rat)", function (req, res) {
res.send('dogs or pinguin');
});
错误:
return new RegExp(path, flags);
^
SyntaxError: Invalid regular expression: /^/(?(?:([^/]+?))|pinguin|bear|wolf|cat|rat)/?$/: Invalid group
at new RegExp (<anonymous>)
at pathtoRegexp (C:UsersCorentinnode_modulespath-to-regexpindex.js:128:10)
at new Layer (C:UsersCorentinnode_modulesexpresslibrouterlayer.js:45:17)
at Function.route (C:UsersCorentinnode_modulesexpresslibrouterindex.js:500:15)
at Function.app.<computed> [as get] (C:UsersCorentinnode_modulesexpresslibapplication.js:481:30)
at Object.<anonymous> (C:UsersCorentinDocumentsmesProjetsmdmmdm-apiroutes.js:36:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
尝试使用以下代码
app.get(/^/(dogs|pinguin|bear|wolf|cat|rat)/, function (req, res) {
res.send('dogs or pinguin');
});