How do you extract form data (form[method="post"]
) and file uploads sent from the HTTP POST
method in Node.js?
I've read the documentation, googled and found nothing.
function (request, response) {
//request.post????
}
Is there a library or a hack?
You can use the querystring
module:
var qs = require('querystring');
function (request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function () {
var post = qs.parse(body);
// use post['blah'], etc.
});
}
}
Now, for example, if you have an input
field with name age
, you could access it using the variable post
:
console.log(post.age);