本文介绍了Express.js路由参数带斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应用程序必须响应以下路线:
/ company /:id
/ pre>
/ company /:id / dir
/ company /:id / dir / dir
这里
/ 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 nopath
specified e.g aroot
directory. I was thinking for something likeapp.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路由参数带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!