micnic/simples

POST data unavailable

Closed this issue · 3 comments

There doesn't seem to be a way to get the POST data. I console.logged the entire connection and can't get the POST params.

app.post('/api', function(connection) {
console.log(connection);
connection.end('ok');
});

Also, I see that if I switch to GET, the requested params are found in url.search, url.path and url.href (as part of the url), in query (as an object). Again, why not in params? I always get an empty params {}, no matter the scenario.

PS: great work on simpleS and simpleT. I'm at the beginning of the road with node.js and these guys help me a lot!

I found a way but it's more or less the same as with the http module, not easier...

var qs = require('querystring');
simples_server.post('/api', function(connection) {
connection.request.on('data', function(data) {
var request = qs.parse(data.toString('utf8'));
// process request, generate result
connection.end(result);
});
});

Hi, I'll try to answer to all your questions :)

There doesn't seem to be a way to get the POST data.

No, there is, the parsed data from POST requests is got from connection.parse(), but it depends on the way the data is sent, in your case with urlencoded data you can use this example:

connection.parse({
  limit: 1024 * 1024, // Here you can set the limit for the request body
  urlencoded: function (form) {
    form.on('end', function () {
      form.result; // use the results
    });

    form.on('error', function (error) {
      // handle errors
    });
  }
});

More info: https://github.com/micnic/simpleS/wiki/Documentation#http-connection-parse

Next version (0.7.0) will have better API for parsing, this example will be simplified to:

connection.parse({
  limit: 1024 * 1024,
  urlencoded: function (error, query) {
    if (error) {
      // handle errors
    } else {
      // use the query
    }
  }
});

Also, I see that if I switch to GET, the requested params are found in url.search, url.path and url.href (as part of the url), in query (as an object). Again, why not in params? I always get an empty params {}, no matter the scenario.

The connection.params object is populated in routes like this:

app.get('/user/:user_id', function () {
  // here you can use connection.params.user_id
});
app.get('/user/:user_id/action/:user_action', function () {
  // here you can use connection.params.user_id and connection.params.user_action
});

These are parameterized routes, these routes are more flexible and allow to create more complex routing logic. More info here: https://github.com/micnic/simpleS/wiki/Documentation#host-routing

PS: great work on simpleS and simpleT. I'm at the beginning of the road with node.js and these guys help me a lot!

Thanks, I'm doing my best to improve these modules, soon will come simpleS 0.7.0 with a lot more features, with a lot of bug fixes and with some performance improvements.

Version 0.7.0 released ;)