String routes in expressjs is like a regular expression so with that being said, you can use asterisk *
as a wildcard:
app.get('/s/:key/*', function(req, res, next) {
var key = req.params.key;
var someOtherParams = req.params[0];
});
Which will be:
- http://example.com/s/1/some/other/stuff -
req.params.key
=1
,req.params[0]
=some/other/stuff
Then from there you can parse your wildcard. Like split it by /
.
OR if you want to be strict that it should not have other characters than alphanumeric, slashes and dashes, use regex directly on your route. Because on expressjs, you can't do a string route containing a single param with slashes then use regex on it and capture that param. It's a bit odd but you can look at this answer for the explanation.
Anyway, for the code to do a regex on your route:
app.get(/s\/([A-Za-z0-9]+)\/(([A-Za-z\-\/]+)*$)/, function(req, res, next) {
var key = req.params[0];
var someOtherParams = req.params[1];
});
Which is capturing 2 groups (req.params[0]
-->([A-Za-z0-9]+)
and req.params[1]
-->(([A-Za-z\-\/]+)*$)
).
The first group is actually your key, and the second group is the param that can contain alpha-numeric, dash and slash. Which you can parse or split by slashes. This way, your route is strict enough to not contain any other characters.
Result will be:
- http://example.com/s/1/some/other/stuff -
req.params[0]
=1
,req.params[1]
=some/other/stuff
- http://example.com/s/1/some-other/stuff -
req.params[0]
=1
,req.params[1]
=some-other/stuff
- http://example.com/s/1/some/other-weird/stuff -
req.params[0]
=1
,req.params[1]
=some/other-weird/stuff
- http://example.com/s/1/some/other/stuff-one -
req.params[0]
=1
,req.params[1]
=some/other/stuff-one
- http://example.com/s/1/some&other/stuff - Error 404,
&
is not permitted in your regex