1. 首先是最基本的用法

app.get("/",function(req,res){ 
    res.send("hello world"); 
}); 

2. 加个路径版的

app.get("/toolmao",function(req,res){ 
    res.send("welcome to toolmao"); 
}); 

3. 稍微为复杂一点的,可以把路径作为参数

app.get('/user/:id', function(req, res){ 
    res.send('user ' + req.params.id); 
}); 

4. 来个正则

app.get(/\/user([^\/]+)\/?/, function(req, res){ 
    res.send(req.params); 
}); 

这个看起来有点复杂哦,解释一下这个正则`/\/user([^\/]+)\/?`
就是/user开始,`\`表示转义,括号里边是说以/开始,不能以/结束,+说明中间可以是任何东西但至少要有,就这些,所以其实和上边的那个是一样的。

5. 选择

app.route('/:url(app|api|modules|lib)')
    .get(core.renderNotFound);

:url(app|api|modules|lib)表示第一个参数应该是四个中的其中之一

6. 通配符

app.route('/app/*')
    .get(core.renderNotFound);

通配符代表任意情况包括空
所以应该是/app/活着的/app/everything

相关文章:

  • 2022-02-23
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
猜你喜欢
  • 2022-12-23
  • 2021-07-18
  • 2021-12-12
  • 2022-02-12
  • 2021-08-24
  • 2021-08-11
  • 2022-12-23
相关资源
相似解决方案