req.body does not work on Express

1

I'm trying to read parameters sent via post, but express only shows in empty console.log 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 route via Postman object

    
asked by anonymous 31.08.2018 / 18:00

1 answer

1

The problem is that you are sending a JSON object with plain text , you need to change this option in Postman :

This will solve

    
31.08.2018 / 18:14