Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get Posted Data From Form

Get Posted Data From Form

Problem

Here is a html form which posts:



Home Html Page

Go To Product
Enter Text:

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('
Enter Text:

'); }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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Get Posted Data From Form

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×