Node Express路由获取请求不起作用

时间:2017-02-06 20:10:12

标签: javascript node.js express

我有一个链接到

的按钮
    <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中传递,但页面仍保留在当前页面上。

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

为了利用该路线,您需要像这样称呼它

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);
});