Express.js路由参数带斜杠 [英] Express.js route parameter with slashes

查看:261
本文介绍了Express.js路由参数带斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



应用程序必须响应以下路线:

  / company /:id 
/ company /:id / dir
/ company /:id / dir / dir
/ pre>

这里 / company /:id 是一条没有路径指定例如目录。我正在考虑像 app.get('/ company /:id /:path',... 这些显然不起作用的东西。



如何定义响应所有示例的路由?

解决方案

使用 / company /:id * (注意尾随的星号)



完整示例

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

express.use(express.router);

express.get('/ company /:id *',function(req,res,next){
res.json({
id:req.param('id'),
路径:req.param(0)
});
});

express.listen(8080);


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?

解决方案

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.param('id'),
        path: req.param(0)
    });    
});

express.listen(8080);

这篇关于Express.js路由参数带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆