node.js,express,如何从post请求的body form-data中获取数据

16 浏览
0 Comments

node.js,express,如何从post请求的body form-data中获取数据

我有一个简单的node.js应用程序,我想从用户那里获取发布的正文。

app.js

var express = require('express');
var app = express();
app.use(express.json());
app.post('/api/user', function (req, res) {
    console.log(req.body);
    console.log(req.body.username);
});
module.exports = app;

server.js

var app = require('./app.js');
var server = app.listen(3000, function () {
    var port = server.address().port;
    console.log('Web App Hosted at http://localhost:%s',port);
});

当我用node server.js启动它时,一切正常。当我用Postman检查它时,

在控制台中,它返回

Web App Hosted at http://localhost:3000
{}
undefined

我安装了最新的express。

我尝试了其他一些方法,比如添加body-parser,添加content-type头部,添加express.urlencoded(),但都没有起作用。我需要像上面图片中的Postman那样从form-data中获取数据。我该怎么做?

0
0 Comments

问题的原因是使用最新的express框架时,不再需要body-parser模块来处理post请求的数据。解决方法是使用multer中间件来解析multipart/form-data类型的数据。具体操作如下:

1. 首先,在项目中安装multer模块:

npm install multer --save

2. 在代码中引入multer模块,并创建一个multer的实例,例如:

var express = require('express');
var app = express();
var multer = require('multer');
var upload = multer();

3. 在app中使用multer来处理不同类型的请求数据:

app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(upload.array()); // for parsing multipart/form-data

4. 在处理post请求的接口中,可以通过req.body来获取请求中的数据,例如:

app.post('/api/user', function (req, res) {
    console.log(req.body);
    console.log(req.body.username);
});

通过以上步骤,可以成功接收到form-data、raw或x-www-form-urlencoded类型的数据。

作者也遇到了类似的问题,花了3个小时才找到解决方法,最后还发现自己忘记在路由中包含multer函数。

0
0 Comments

问题的原因是发送的请求是一个form-data请求,而不是json请求。因此,使用express.json()方法无效。解决方法是安装并使用body-parser来解析req.body。

首先,需要安装body-parser模块。可以使用以下命令进行安装:

npm install body-parser

然后,在代码中引入body-parser模块,并将其作为中间件使用。以下是示例代码:

var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

这段代码将会解析请求的整个body部分,并将其暴露在req.body中,以供后续处理使用。

通过安装和使用body-parser模块,就可以正确地解析form-data请求中的数据了。

0
0 Comments

在Express的API文档中指定了必须使用提供的中间件之一来给请求体(body)赋值。他们做出这个决定是因为HTTP请求体可以采用许多不同的格式,他们不想假设您的应用程序使用哪种格式。

One of the middlewares that Express provides is the `body-parser` middleware. This middleware is responsible for parsing the body of the request and populating the `req.body` object with the parsed data. To use `body-parser`, you need to install it first by running the following command:

npm install body-parser

Once `body-parser` is installed, you can require it in your Express application and use it as a middleware. Here is an example of how to use `body-parser` to parse form data in a POST request:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/form', (req, res) => {
  const formData = req.body;
  // Do something with the form data
  res.send('Form data received');
});
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

In this example, the `bodyParser.urlencoded()` middleware is used to parse the form data. The `extended: true` option allows parsing of nested objects in the form data.

With this setup, when a POST request is made to the `/form` endpoint, the form data will be available in the `req.body` object. You can then access the form data and process it as needed.

By using the `body-parser` middleware, you can easily parse and access form data in Express. This allows you to handle form submissions and extract data from the request body effectively.

0