简体   繁体   中英

I can not get request in body format in Node.js

I have problems receiving request in body format on my server.

I am using Express version 4.17.1. The documentation indicates that I don't have to use body-parserer, but I can do it directly with the express functionality "express.json ()"

However, I've been trying to get it to work for a long time but nothing happens: the console does not show anything. It seems that it does not recognize the request at all.

I'm doing all the request from Postman in body format JSON.

This is my code:

const express = require("express");
const formidable = require("express-formidable");
const cors = require("cors");
const dotenv = require("dotenv").config();

const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(formidable());
app.use(cors());

(...)

app.post("/list", async (req, res) => {
  try {
    console.log(req.body);
  } catch (error) {
    return res.status(400).json({ message: error.message });
  }
});

What am I doing wrong?

Thank you very much for your time and help in advance.

Express parse request body according to defined type in Content-Type header. Try to do request with header Content-Type: application/json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM