Node JS: Where is my POST data?

I have no shortage of sending POST request to node js server. I have a simple request and a simple server.

My server code:

var http = require('http');
var bodyParser   = require('body-parser');
http.createServer(function (req, res) {

console.log(req.body);

res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/')

      

my client request code:

var val = JSON.stringify({ city:"SomeCity", name:"MyNameIsHere" });
alert(val);
$.ajax({
     url: 'http://127.0.0.1:1337',
     type: 'POST',
     data: { value:  val},
     success: function(result) {
         alert('the request was successfully sent to the server');}
});

      

So my guess is to get SomeCity and MyNameIsHere strings in the request body on the node js server, but the req.body field is undefined. I must say that I open my test.html with request code locally with a url like this:

file:///D:/Projects/test.html

      

Maybe I'm blind and in control of something trivial, but I have no idea what :)

+3


source to share


3 answers


I must say that I open my test.html with request code locally with a url like this: file:///D:/Projects/test.html

You are trying to post cross domain which you cannot do in this case. Serve your HTML over HTTP so you can POST. If you are using browser development tools, you will see that the request never reaches your Node.js server (except for a possible preflight request for CORS).



Another problem is that you are not actually using body-parser

. If you need data for a message, you will need to read from req

as a stream.

+2


source


You include "body-parser" in var bodyParser = require('body-parser');

, but you never use it. It won't magically parse the request for you. By default, the object request

has no property body

... see the documentation .

If you want to use a module body-parser

, you should probably use express.js

and read the documentation on how to wire it up as middleware.



If you want to get the body using the built-in server http

, you first need to use the object request

using something like this:

if (req.method == 'POST') {
    var body = '';
    req.on('data', function(data) {
        body += data;
        if (body.length > 1000000) {
            req.connection.destroy();
        }
    });
    req.on('end', function () {
        console.log(req.body);

        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World\n');
    });
}

      

0


source


Adding express library and bodyparser as middleware. However, I could use the code from neelsg , but working with the built-in HTTP server and handling the mail data on my own is too cumbersome. So, part of my working code is here:

var express = require('express');
var http    = require('http');
var url     = require('url');
var bodyParser   = require('body-parser');

var app = express();
app.use(express.bodyParser(
{
    keepExtensions: true,
    limit: 30*1024*1024 // lets handle big data  
}
));
app.use(bodyParser.urlencoded());

      

Bodyparser by default can only handle 100Kb data in a post request, so I had to increase the limit using its config options.

0


source







All Articles