Node.js 如何在NodeJS中允许表单数据

Node.js 如何在NodeJS中允许表单数据,node.js,api,postman,Node.js,Api,Postman,我最近创建了一个接受文件的API。我正在尝试使用Postman测试API。如果我使用x-ww-form-urlencodedbody type发出post请求,那么一切都正常,我得到了所有预期的数据。唯一的问题是它不允许发送文件。如果我使用允许您发送文件的表单数据主体类型,我在后端不会收到任何内容。我不确定邮递员是否出了问题,或者我是否做错了什么。我的直觉是后端当前不接受表单数据,这就是我没有收到任何数据的原因。我能做些什么来改变这一点 到目前为止,我的标题看起来像这样 res.setHeade

我最近创建了一个接受文件的API。我正在尝试使用Postman测试API。如果我使用
x-ww-form-urlencoded
body type发出post请求,那么一切都正常,我得到了所有预期的数据。唯一的问题是它不允许发送文件。如果我使用允许您发送文件的
表单数据
主体类型,我在后端不会收到任何内容。我不确定邮递员是否出了问题,或者我是否做错了什么。我的直觉是后端当前不接受
表单数据
,这就是我没有收到任何数据的原因。我能做些什么来改变这一点

到目前为止,我的标题看起来像这样

res.setHeader('Access-Control-Allow-Origin', origin);
res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, form-data");

app.js

app
    // static route will go to the client (angular app)
    .use(express.static('client'))

    // secured routes
    .use('/api', secured_route)

    // add a user route
    .post('/user', user_api_controller.add_user)

    // delete this in the production
    .use(function(req, res, next) {
        res = allowed_orgins(req, res);
        next();
    })
;

allowed_orgins = function (req, res){
    var allowedOrigins = ['http://localhost:4200', 'http://localhost:8100'];
    var origin = req.headers.origin;
    if(allowedOrigins.indexOf(origin) > -1){
        res.setHeader('Access-Control-Allow-Origin', origin);
        res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, multipart/form-data");
    }
    return res;
}
module.exports.add_user = function (req, res) {
    console.log(req.body.username);
    console.log(req.body.other_parameters);
}
module.exports.add_user = function (req, res) {
    console.log(req.file);
}
user\u api\u controller.js

app
    // static route will go to the client (angular app)
    .use(express.static('client'))

    // secured routes
    .use('/api', secured_route)

    // add a user route
    .post('/user', user_api_controller.add_user)

    // delete this in the production
    .use(function(req, res, next) {
        res = allowed_orgins(req, res);
        next();
    })
;

allowed_orgins = function (req, res){
    var allowedOrigins = ['http://localhost:4200', 'http://localhost:8100'];
    var origin = req.headers.origin;
    if(allowedOrigins.indexOf(origin) > -1){
        res.setHeader('Access-Control-Allow-Origin', origin);
        res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, multipart/form-data");
    }
    return res;
}
module.exports.add_user = function (req, res) {
    console.log(req.body.username);
    console.log(req.body.other_parameters);
}
module.exports.add_user = function (req, res) {
    console.log(req.file);
}
如果我使用
表单数据
,我在控制台上不会得到任何打印结果,但当我使用
x-ww-form-urlencoded
时,它工作正常



我用了
multer中间件
,但还是没有得到任何东西。当我的后端收到一些东西时,中间件就开始发挥作用。我已经尝试获取
表单数据的纯文本字段
,但我也没有得到。这意味着我的后端无法接收所有
表单数据
字段,不仅是文件,还有文本字段。

文件上载的内容类型头实际上是
多部分/表单数据
而不是
表单数据
。要在Postman中适当上载文件,请查看以下内容:

在后端,您需要一个库来处理
多部分/表单数据
请求。如果您使用的是expressjs,那么这个库将满足您的需要

以下是如何使用
multer

1) 首先安装multer

npm install --save multer
2) 在代码中包含该库(请注意,上载目录必须存在)

3) 使用
upload
作为
add_user
端点的中间件(用包含上载文件的字段名填充括号)

4) 该文件可以在您的用户\u api\u controller.js中访问

app
    // static route will go to the client (angular app)
    .use(express.static('client'))

    // secured routes
    .use('/api', secured_route)

    // add a user route
    .post('/user', user_api_controller.add_user)

    // delete this in the production
    .use(function(req, res, next) {
        res = allowed_orgins(req, res);
        next();
    })
;

allowed_orgins = function (req, res){
    var allowedOrigins = ['http://localhost:4200', 'http://localhost:8100'];
    var origin = req.headers.origin;
    if(allowedOrigins.indexOf(origin) > -1){
        res.setHeader('Access-Control-Allow-Origin', origin);
        res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, multipart/form-data");
    }
    return res;
}
module.exports.add_user = function (req, res) {
    console.log(req.body.username);
    console.log(req.body.other_parameters);
}
module.exports.add_user = function (req, res) {
    console.log(req.file);
}

下面是我的超级简单express示例:

const express = require('express')
const fileUpload = require('express-fileupload');

const app = express()
app.use(fileUpload());

app.post('/file-upload', function(req, res, next) {
  console.log(req.body.msg);
  console.log(req.files);
  res.send('ok');
  next();
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})
对不起,我没有使用postman,但我使用的是
curl
,它对我很有用:

curl http://localhost:3000/file-upload \
  -H "Content-Type: multipart/form-data" \
  -F "file=@/YourDir/YourFile.txt" \
  -F "msg=MyFile"

因此,您可以使用此curl命令尝试、测试、获取要点并使用您的应用程序。

我已将标题类型更改为
multipart/form data
。不过,它并没有向我显示任何在后端接收数据的迹象。我在我原来的帖子中加入了更多的代码。请检查我是否在那里做错了什么。要在后端接收文件,您需要使用另一个库
multer
。我已经更新了我的答案以给出一些指示。仍然没有得到任何信息。虽然它创建了上载文件夹,但它不上载文件。当我使用
表单数据
时,不仅仅是文件,它也不会获取文本字段。您是通过邮递员
表单数据
还是
多部分/表单数据
发送的?您在后端遇到了什么错误?我通过
表单数据发送了
,没有收到任何错误。当我尝试打印字段时,它会打印
未定义的
,您需要将表单数据传递给req.body表单@HafizTemuri@muthukumar对不起,我没听清楚。你能给我详细解释一下吗?你的问题不在于标题,请把multer Middle ware递给我。我使用了
multer Middle ware
,我仍然没有得到任何东西。当我的后端收到一些东西时,中间件就开始发挥作用。我已经尝试获取
表单数据的纯文本字段
,但我也没有得到。这意味着我的后端无法接收
表单数据
字段,不仅是文件,还有文本字段。使用您的解决方案,当我在Postman中使用
表单数据
时,至少我能够获取文本字段,我说这是一个进步。但是,我仍然无法获得该文件。我尝试使用curl,但它给了我一个错误,
Invoke-WebRequest:Cannot bind参数'Headers'。无法将类型为“System.String”的“内容类型:多部分/表单数据”值转换为类型为“System.Collections.IDictionary”
。有没有办法解决这个问题?无法重现这个错误。。。你还使用什么中间工具?我没有使用任何中间工具。我用的是
multer
,但现在不用了。忘了curl吧……哈哈,我和邮递员一起用的,但我遇到了另一个问题。我无法将文件保存到服务器。我正试图按照本教程的要求保存文件,并提供如图所示的路径<代码>https://www.npmjs.com/package/express-fileupload
类似于这样的东西,
/upload/filename.txt
,但由于某些原因,它构成了自己的路径名。它假设在应用程序的根目录中查找名为
upload
的目录,而不是转到我的硬盘根目录<代码>错误:enoint:没有这样的文件或目录,请打开“C:\uploads\filename.txt”
有什么办法可以解决这个问题吗?我已经让它正常工作了。非常感谢你的帮助。你应该得到我的50分。