Home > Enterprise >  Node/Express: res.body is undefined
Node/Express: res.body is undefined

Time:11-08

(I've already read the answers on other similar OPs and they don't solve my problem. I am using express.json() already)

In the code below, res.body is undefined.

app.use(cors({
   origin: '*',
   credentials: true,
   optionSuccessStatus: 200
}))

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:4200')
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
  res.header('Access-Control-Allow-Methods', 'POST, GET')
  next()
})

app.use(cookieParser())

app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ extended: true, limit: '50mb' }))

app.use(bodyParser.urlencoded({ extended: true }))

app.post('/new-data', ensureLoggedIn('/auth/facebook'), (req, res) => {
  console.log(res.body) // logs undefined
})

What I'm sending to the server is:

fetch('/new-data', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ data })
})

where data is an array of 8 images as Base64 strings, around 1.5Mb each, and I can see the POST request going out correctly on my Chrome Network Panel.

CodePudding user response:

Because you are getting res.body , you should get req.body.

  • Related