9951 explained code solutions for 126 technologies


expressjsHow can I use Express.js to parse JSON data?


Express.js is a web application framework for Node.js that makes it easier to create web applications and APIs. It can be used to parse JSON data in a few different ways.

  1. Using body-parser middleware:
const express = require('express')
const bodyParser = require('body-parser')

const app = express()

// parse application/json
app.use(bodyParser.json())

app.post('/', (req, res) => {
  const data = req.body
  console.log(data)
  // { name: 'John', age: 30 }
})
  1. Using express.json() middleware:
const express = require('express')

const app = express()

// parse application/json
app.use(express.json())

app.post('/', (req, res) => {
  const data = req.body
  console.log(data)
  // { name: 'John', age: 30 }
})
  1. Using express.text() middleware:
const express = require('express')

const app = express()

// parse application/json
app.use(express.text())

app.post('/', (req, res) => {
  const data = JSON.parse(req.body)
  console.log(data)
  // { name: 'John', age: 30 }
})
  • body-parser is a third-party middleware library that can be used to parse incoming request bodies.
  • express.json() is a built-in middleware that can be used to parse incoming request bodies in JSON format.
  • express.text() is a built-in middleware that can be used to parse incoming request bodies in plain text format.

Helpful links

Edit this code on GitHub