Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js route parameter with slashes [duplicate]

I have an application which serves file listings.

The application must respond to following routes:

/company/:id
/company/:id/dir
/company/:id/dir/dir

Here /company/:id is a route with no path specified e.g a root directory. I was thinking for something like app.get('/company/:id/:path', ... which obviously doesn't work.

How can I define a route which responds to all of the examples?

like image 889
Gerstmann Avatar asked May 30 '13 06:05

Gerstmann


1 Answers

Use /company/:id* (note trailing asterisk).

Full example

var express = require('express')();

express.use(express.router);

express.get('/company/:id*', function(req, res, next) {
    res.json({
        id: req.params['id'],
        path: req.params[0]
    });    
});

express.listen(8080);
like image 112
Prinzhorn Avatar answered Nov 03 '22 21:11

Prinzhorn