0

If I use the barebones echo server demo for restify, it works correctly. But if I make a few changes shown below, it behaves different than I would expect:

var restify = require('restify');

function respond(req, res, next) {
  res.send(req.params); //NOTE: should echo back all params
}

var server = restify.createServer();
server.get('/hello/', respond); //NOTE: parameter :name removed

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

What I've done is removed the name param from the URL and echoed all parameters in response function. calling:

http://localhost:8080/hello

returns

{}

but so does:

http://localhost:8080/hello?foo=bar

Why don't I see foo:bar in the response?

Rich Sadowsky
  • 966
  • 1
  • 12
  • 22

2 Answers2

0

Everything that goes after the question mark in the URL are not params but query, and in order to reference it use: req.query.
Try this to echo back all data.

function respond(req, res, next) {
  res.send({
    params: req.params,
    query: req.query
  });
}
Community
  • 1
  • 1
moka
  • 22,846
  • 4
  • 51
  • 67
  • oh thank you. I like that even better than my previous answer! – Rich Sadowsky Jul 03 '13 at 16:01
  • btw, I was originally doing this via POST and was seeing inconsistencies such as when the :name param was in the mapped URL I DID get the query AND params with both POST and GET. That is what confused me. – Rich Sadowsky Jul 05 '13 at 20:01
  • For POST data, you need to use `req.body`. – moka Jul 06 '13 at 14:48
  • thanks again for the help Maksims. I will go back and double check the original results that led me to think I should get the params (or body) back. If I can reproduce I may have stumbled on a subtle bug. The behavior should be consistent with or without a variable component of the mapped URL. – Rich Sadowsky Jul 07 '13 at 10:37
0

I found helpful answers in other post.

Ultimately I needed to add this for GETs

server.use(restify.queryParser());

and

server.use(restify.bodyParser());

for POSTs

Rich Sadowsky
  • 966
  • 1
  • 12
  • 22