0

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?

3 Answers 3

3

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);
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.