ℙℕℤℝ 2013-06-09 08:51
浏览 247
已采纳

如何在"?"之后访问 GET 参数 在《快报》上?

I know how to get the params for queries like this:

app.get('/sample/:id', routes.sample);

In this case, I can use req.params.id to get the parameter (e.g. 2 in /sample/2).

However, for url like /sample/2?color=red, how can I access the variable color?

I tried req.params.color but it didn't work.

转载于:https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express

  • 写回答

8条回答 默认 最新

  • 程序go 2013-06-09 08:56
    关注

    So, after checking out the express reference, I found that req.query.color would return me the value I'm looking for.

    req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?'

    Example:

    GET /something?color1=red&color2=blue
    

    Then in express, the handler:

    app.get('/somthing',(req,res) => {
        req.query.color1 === "red";  //true
        req.query.color2 === "blue"; // true
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容