gpt4 book ai didi

javascript - 如何使用 multer 将文件从 postman 发送到 node.js

转载 作者:数据小太阳 更新时间:2023-10-29 04:00:33 24 4
gpt4 key购买 nike

window
express 4.12.4
穆尔特 1.0.1
Node v0.10.22

我正在尝试使用 postman 将文件发送到我的 node.js 服务器。

我正在尝试遵循自述文件 here

这是我用 postman 发送的内容:

POST /ingest HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Postman-Token: 69dd2497-2002-56ed-30a4-d662f77dc0b0
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

这是它的样子: enter image description here

这是它在 node.js 上的作用

var Ingest      = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
});

module.exports = function (app) {
app.post('/ingest', upload.single('test'), function(req, res, next) {
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
}

当我用 postman 点击这条路线时,我得到 {} for req.bodyundefined for req.file.

我做错了什么?

这里是我初始化传递给路由文件的 app 的地方:

var express = require('express');
var app = express();
var http = require('http');
var cfg = require('./config')();
var passport = require('passport');
var cors = require('cors');

var bodyParser = require('body-parser');

app.set('port', process.env.PORT || cfg.port);
var corsOptions = {
origin: "*",
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'x-reset-token', 'x-invite-token', 'x-api-key', 'x-www-form-urlencoded'],
credentials: true
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

也许里面有什么东西在做?

编辑:

我试过了

var Ingest      = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
}).single('csv');

module.exports = function (app) {

app.post('/ingest', function(req,res){
upload(req, res, function(err) {
if(err){
console.log(err);
}
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
});

}

但这并没有帮助。它不会为 err

记录任何内容

最佳答案

只需从 postman 中删除 header Content-Type:application/x-www-form-urlencoded

enter image description here

关于javascript - 如何使用 multer 将文件从 postman 发送到 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com