Windows
Express 4.12.4
Multer 1.0.1
节点 v0.10.22
我正在尝试使用邮递员将文件发送到我的 node.js 服务器。
我正在尝试按照这里的自述文件
这是我与邮递员一起发送的内容:
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
这是它对 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);
});
});
}
当我和邮递员一起打这条路线时,我得到{}
了 forreq.body
和undefined
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