快速路由器-:id? [英] Express router - :id?

查看:87
本文介绍了快速路由器-:id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真正简单的问题伙计们:我看到很多书籍/代码片段在路由器中使用以下语法:

Real simple question guys: I see a lot of books/code snippets use the following syntax in the router:

app.use('/todos/:id', function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

我不确定如何在此处解释路线...它将路线'/todos/anything'吗?然后抓住任何东西"并以可变ID对待?如何使用该变量?我敢肯定,这是一个快速的答案,我之前从未见过这种语法.

I'm not sure how to interpret the route here... will it route '/todos/anything'? and then grab the 'anything' and treat is at variable ID? how do I use that variable? I'm sure this is a quick answer, I just haven't seen this syntax before.

推荐答案

这是一个快速的中间件.

This is an express middleware.

在这种情况下,是的,它将路由/todos/anything,然后将req.params.id设置为'anything'

In this case, yes, it will route /todos/anything, and then req.params.id will be set to 'anything'

这篇关于快速路由器-:id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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