在快速路线api中添加命名参数 [英] Adding named parameters in express route api

查看:56
本文介绍了在快速路线api中添加命名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条类似这样的快速路线:

I have an express route that looks like this:

app.get('/api/v1/username/:option', function(req, res) {

  // do stuff

})

如何修改此路由,以便URL显示选项(option=)的参数名称?例如:

How can I modify this route so that the URL show the parameter name of option (option=)? For example:

http://localhost:8080/api/v1/johndoe/option=my-cool-option

推荐答案

这是一个URL段,而不是参数.

That's a URL segment, not a parameter.

如果您想要显示网址,就像它一样

If you want it like you've shown the URL, it'd be

http://localhost:8080/api/v1/johndoe/?option=my-cool-option

请注意问号?,它指定它是GET参数.

Note the question mark ?, this specifies that it's a GET parameter.

app.get('/api/v1/:username', function(req, res) {
    //req.params.username would equal 'johndoe'
    //req.query.option would equal 'my-cool-option'
})

这篇关于在快速路线api中添加命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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