【问题标题】:Node Express routes get request not workingNode Express 路由获取请求不起作用
【发布时间】:2017-06-23 21:41:29
【问题描述】:

我有一个链接到的按钮

    <a href="/data_entry?Display=A&ts=<%=dateStamp%><%=locTimeStamp%>">A</a>

我的路线,

router.get('/data_entry/:Display/:ts', function(req,res){
console.log('get display');
 });

没有在点击时被调用。链接在 url 中传递,但页面停留在当前页面上。

【问题讨论】:

  • 您指定了一个应该用作/data_entry/something/somethingelse 的路径并尝试使用/data_entry?foo=something&amp;bar=somethingelse

标签: javascript node.js express


【解决方案1】:

为了使用该路由,您需要这样调用它

curl -X GET "http://localhost:3000/data_entry/A/<%=dateStamp%><%=locTimeStamp%>"

请务必正确编码 URI。见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

如果您想使用查询参数而不是命名参数,您可以以不同的方式定义您的路由,然后使用查询参数。见:http://expressjs.com/en/4x/api.html#req.query

看起来像这样:

app.get('/data_entry', function (req,res){
    console.log('get display', req.query);
});

【讨论】:

    【解决方案2】:

    Display 和 ts 作为变量传递给请求对象 (req),因此为了从 url 访问值,它们将存储在 req.query 中

    router.get('/data_entry', function(req, res){
        // req.query will store the display and ts values
        console.log(req.query.Display);
    }
    

    通过这些更改,您的代码将按预期运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多