简体   繁体   中英

Node Express routes get request not working

I have a button that links to

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

My route,

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

Is not being called on click. The link is being passed in the url but the page stays on the current page.

The Display and ts is passed as variables to the request object (req) so in order to access the values the from the url they will be stores in req.query

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

With these changes your code will function as expected

In order to utilize that route, you need to call it like this

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

Be sure to also properly encode the URI. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

If you want to use query parameters instead of named parameters, you would define your route differently and then utilize the query parameters. See: http://expressjs.com/en/4x/api.html#req.query

It would look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM