如何在 NodeJS 中允许表单数据 [英] How to Allow Form-Data in NodeJS
问题描述
我最近创建了一个接受文件的 API.我正在尝试使用 Postman 测试 API.如果我使用 x-wwww-form-urlencoded
正文类型发出发布请求,则一切正常,我将获得所有预期数据.唯一的问题是它不允许发送文件.如果我使用 form-data
正文类型,它允许您发送文件,我在后端不会收到任何东西.不确定 Postman 是否有问题,或者我做错了什么.我的预感是后端目前不接受 form-data
,这就是为什么我没有收到任何数据.我可以做些什么来改变它?
I recently created an API that accepts files. I am trying to test the API using Postman. If I make a post request using x-wwww-form-urlencoded
body type, everything works and I get all the expected data. The only problem is that it does not allow to send a file. If I use form-data
body type, that allows you to send files, I don't receive anything at the back-end. Not sure if something is wrong with Postman or if I am doing something wrong. My hunch is that the back-end doesn't accept form-data
currently, that is why I am not receiving any data. Anything I could do to change that?
到目前为止,我的标题看起来像这样,
My headers look something like this so far,
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;
}
user_api_controller.js
module.exports.add_user = function (req, res) {
console.log(req.body.username);
console.log(req.body.other_parameters);
}
如果我使用 form-data
,我在控制台上什么也没有打印出来,但是当我使用 x-wwww-form-urlencoded
时它工作正常.
I get nothing print out on console if I use form-data
, but it works fine when I use x-wwww-form-urlencoded
.
我使用了 multer 中间件
,我仍然没有得到任何东西.当我的后端收到一些东西时,中间件就开始发挥作用了.我曾尝试获取 form-data
的纯文本字段,但我也没有得到.这意味着我的后端无法接收所有 form-data
字段,不仅是文件,还有文本字段.
I've used multer middle ware
, I am still not getting anything. Middle ware comes into play when my back-end will receive something. I have tried getting the plain text fields of form-data
, and I am not getting that either. That means my back-end is unable to receive all form-data
fields, not just files but the text fields as well.
推荐答案
这里是我的超级简单的表达示例:
Here my super simple express example:
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!')
})
对不起,我不使用邮递员,但我使用 curl
并且它对我有用:
Sorry I don't use postman, but I use curl
and it works for me:
curl http://localhost:3000/file-upload \
-H "Content-Type: multipart/form-data" \
-F "file=@/YourDir/YourFile.txt" \
-F "msg=MyFile"
这样您就可以尝试、测试、获取要点并使用此 curl 命令玩您的应用程序.
So you can try it, test it, get the gist and play with your app using this curl command.
这篇关于如何在 NodeJS 中允许表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!