简体   繁体   English

我无法在 Node.js 中获取正文格式的请求

[英]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.我使用的是Express 版本 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 ()"文档表明我不必使用 body-parserer,但我可以直接使用 express 功能“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.我正在以正文格式 JSON 完成来自 Postman 的所有请求。

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.根据Content-Type header 中定义的类型快速解析请求体。 Try to do request with header Content-Type: application/json .尝试使用 header Content-Type: application/json进行请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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