Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs not receiving POST body

I'm sending a POST in angularjs like so:

$http.post("/mypath", { data: "foobar" })

And in nodejs (expressjs) I'm trying to pick it up like so:

app.post "/mypath", (req, res) ->
  console.log "req.body: ", req.body
  res.end()

I've tried various different incarnations (body: "foobar", etc), but I keep getting req.body: undefined

Is there a simple way to read the payload in node/express?

like image 212
Tyler Avatar asked Mar 28 '26 21:03

Tyler


1 Answers

To get data from a POST in Node, you need to use a body Parser. eg:

var bodyParser = require('body-parser');

//use bodyParser() to let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
like image 86
Riron Avatar answered Mar 31 '26 11:03

Riron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!