Get URL parameter from POST method in NodeJS / Express

I want to do the following, but I do not know if this is possible if he did not perform string manipulations in the URL. I want to get some parameter from url, I have some data that is transmitted in the mail data, but I need to get some information (user ID) from the URL:

  • I use express
  • I use post method
  • My url is like:

http://www.mydomain.com/api/user/123456/test?v=1.0

I have the following code to get all mail requests:

var http = require('http');
var url = require('url') ;
exp.post('*', function(req, res, next) {
     var queryObject = url.parse(req.url,true).query; // not working only get in the object the value v=1.0
     var parameter = req.param('name'); // get undefined

}

What am I missing?

thank

+4
source share
1 answer

GET parameters (as an object) are in req.query. Try:

exp.post('*', function(req, res, next) {
  console.log(req.query);
  console.log(req.query.v);
  next();
});

-, URL. req.params:

exp.post('api/user/:userid', function(req, res, next) {
  console.log(req.params);
  console.log(req.params.userid);
  next();
});
+3

Source: https://habr.com/ru/post/1530088/


All Articles