DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

Node.js get json post data

I use express.js on my server. From my client I try to:

$http.post("url/send", angular.toJson(
    {
        uploads: uploads, 
        desc: desc
    }
));

On the server I want to read this data:

send function(req, res, next){

};

How can I extract the posted json string from the req object?

Upvotes: 0

Views: 4340

Answers (3)

Dolcekar
Dolcekar

Reputation: 39

The above solutions have been deprecated in Express 4. Note that configure is no longer used to set up middleware. Secondly, bodyParser is no longer part of Express. Instead bodyParser is its own entity package and should be called separately https://www.npmjs.com/package/body-parser the code:

    app.use(express.bodyParser());
    });

is in Express 4:

app.use(bodyParser()); 

(much simpler!)

Upvotes: 0

Jonas Liljestrand
Jonas Liljestrand

Reputation: 166

You need to add the bodyParser in your express setup like this

app.configure(function () {
    app.use(express.bodyParser({ keepExtensions: true }));
});

Then in your route/middleware u just reed the data in req.body

Upvotes: 2

moka
moka

Reputation: 23047

In Express add bodyParser middleware in configure:

app.configure(function() {
  app.use(express.bodyParser());
});

And then in any request, req.body will contain your JSON with body data:

app.post('/items', function(req, res, next) {
  console.log(req.body);
});

Upvotes: 3

Related Questions