req.body nãp works on Express

Asked

Viewed 471 times

1

I’m trying to read parameters sent via post, but the express only shows on the console.log empty objects.

This is the code

import * as jsonServer from 'json-server'
import {Express} from "express";
import * as fs from 'fs'
import * as https from 'https'

const server: Express = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()

// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares)


// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser)

server.post('/login', (req, res, next) =>{
  console.log(req.body)
})
 // --
// Use default router
server.use(router)
const options = {
  cert: fs.readFileSync('./backend/keys/cert.pem'),
  key: fs.readFileSync('./backend/keys/key.pem')
}
https.createServer(options, server).listen(3001, () => {
  console.log('JSON Server is running on https://localhost:3001')
})

I’m trying to see as Postman’s object

inserir a descrição da imagem aqui

  • already tried to change in the Postman from "Text" to "JSON"?

  • Where do I make that change?

  • Ah found and received the parameters

  • I will post as an answer

  • All right, thanks!..

1 answer

1


The problem is you’re sending an object JSON with Plain text, need to change this option on Postman:

inserir a descrição da imagem aqui

That will solve

Browser other questions tagged

You are not signed in. Login or sign up in order to post.