Alexander Kleinhans Alexander Kleinhans - 2 years ago 116
Javascript Question

Get post data in NodeJS

I believe the answer to this question is out of date because of this answer.

I've read solutions about a npm packaged called body-parser, but I don't want to use it if I can find another way. I just simply want to parse POST data in node.

I have an ajax function like this:

$.ajax {
url: '/foo',
method: 'POST',
data: {foo: "foo", bar: "bar"}

something like:'/foo', function(req, res) {
var postFoo =; // How do I do this?

Answer Source

You can use the body-parser middleware on your Express route:

$ npm install body-parser --save


const express = require('express')
const bodyParser = require('body-parser')

const app = express()

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())'/foo', function (req, res) {
  // req.body is a plain object
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download