Routes regexps support
Closed this issue · 8 comments
Seems like you are currently not supporting the same format of describing route source as Now itself.
In my case, this throws errors trying to run locally, but work perfectly fine on Now:
"routes": [
{ "src": "/api/entity/(?<name>[^/]*)", "dest": "/api/entity.js?name=$name" },
{ "src": "/api/collection/(?<name>[^/]*)", "dest": "/api/collection.js?name=$name" }
],
Docs: https://zeit.co/docs/v2/deployments/configuration/#routes
Stacktrace:
/Users/igloczek/.nvm/versions/node/v8.11.3/lib/node_modules/now-lambda-runner/index.js:43
const r = new RegExp(route.src);
^
SyntaxError: Invalid regular expression: //api/entity/(?<name>[^/]*)/: Invalid group
at new RegExp (<anonymous>)
at nowConf.routes.forEach.route (/Users/igloczek/.nvm/versions/node/v8.11.3/lib/node_modules/now-lambda-runner/index.js:43:15)
at Array.forEach (<anonymous>)
at Object.<anonymous> (/Users/igloczek/.nvm/versions/node/v8.11.3/lib/node_modules/now-lambda-runner/index.js:39:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
Good spot. I thought that now uses regular regular expressions :) It looks like this is not the case. I'll have to dig a little bit to see how how resolve the URL.
In the meantime maybe:
"routes": [
{ "src": "/api/entity/(.*)", "dest": "/api/entity.js?name=$1" },
{ "src": "/api/collection/(.*)", "dest": "/api/collection.js?name=$1" }
]
@Igloczek can you please try the latest 1.0.0
version. It should work now. I tested with the following:
"routes": [
{ "src": "/e/resources/(?<resource>[^/]*)", "dest": "/www/static/app/resources/$resource"}
]
hi @krasimir
I tried version 1.
It seems like it matches but then does not resolve?
If I use this expression>
"routes": [{ "src": "/api/(?<resource>[^/]*)", "dest": "/api/$resource" }]
Just released 1.1.0 and should be ok ;)
I have:
{ "src": "/api/(?<resource>[^/]*)", "dest": "/api/$resource/index.js?name=$resource" },
And the following /api/test/index.js
file:
const { parse } = require('url');
module.exports = (req, res) => {
const { query } = parse(req.url, true);
res.send('hello world ' + query.name);
}
The result for the http://localhost:8004/api/test is
ah nice! thank you very useful
Thanks! It doesn't throw errors about routes syntax, so I can assume it works, but something else doesn't work for me... If I found what is wrong will let you know, maybe it's just something on my side.