Get Posted Data From Form
Problem
Here is a html form which posts:
Home Html Page
Go To Product
Here is JavaScript code which should get Posted data:
function Server(req, resp) {
if (req.method == 'POST') {
var chunk = '';
req.on('data', function (data) {
chunk += data;
});
req.on('end', function () {
console.log(chunk + "
Can you help me? I'm new in nodejs, so can you show me example?
Problem courtesy of: karaxuna
Solution
You have to specify a name
on your element like this:
only those with name
set get posted.
Example:
var http=require('http');
var util=require('util');
var querystring=require('querystring');
var server=http.createServer(function(req,res){
if (req.method=='GET'){
res.end('
'); }else{ var chunk = ''; req.on('data', Function (data) { chunk += data; }); req.on('end', function () { console.log(chunk + "
To parse the formdata just use querystring.parse(str, [sep], [eq]) (like in the example above)
Example:
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
Solution courtesy of: stewe
Discussion
View additional discussion.