在URL中使用多个参数

编程入门 行业动态 更新时间:2024-10-28 18:26:12
本文介绍了在URL中使用多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Express with Node,我有一个要求,用户可以在其中请求URL: http:// myhost / fruit / apple / red 。

这样的请求将返回JSON响应。

上述电话之前的JSON数据如下所示:

{fruit:{apple:foo} }

根据上述要求,响应JSON数据应为:

{apple:foo,color:red}

我已经配置了快递路由,如下所示:

app.get('/ fruit /:fruitName / ',function(request,response){ / *如上所述使用request.params.fruitName和 request.params.fruitColor返回响应JSON数据,以获取果苹果并将其颜色更新为红色* / });

但这不行。我不确定如何传递多个参数,也就是说,我不确定 / fruit /:fruitName /:fruitColor 是否是正确的方法。是吗?

解决方案

app.get('/ fruit /:fruitName / ',function(req,res){ var data = {fruit:{apple:req.params.fruitName,color:req。 params.fruitColor } }; send.json(data); });

如果不行,请尝试使用console.log(req.params)查看它正在给你。

I am using Express with Node and I have a requirement in which the user can request the URL as: myhost/fruit/apple/red.

Such a request will return a JSON response.

The JSON data, before the above call looks like:

{ "fruit": { "apple": "foo" } }

With the above request, the response JSON data should be:

{ "apple": "foo", "color": "red" }

I have configured express to route as follows:

app.get('/fruit/:fruitName/:fruitColor', function(request, response) { /*return the response JSON data as above using request.params.fruitName and request.params.fruitColor to fetch the fruit apple and update its color to red*/ });

But this does not work. I am unsure of how to pass multiple parameters, that is, I am unsure if /fruit/:fruitName/:fruitColor is the correct way to do this. Is it?

解决方案

app.get('/fruit/:fruitName/:fruitColor', function(req, res) { var data = { "fruit": { "apple": req.params.fruitName, "color": req.params.fruitColor } }; send.json(data); });

If that doesn't work, try using console.log(req.params) to see what it is giving you.

更多推荐

在URL中使用多个参数

本文发布于:2023-11-23 09:43:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   参数   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!