使用formidable模块报错,求大神解答
今天在做一个图片上传的功能,在用formidable的时候遇到一个问题,查了很多资料,但是渣渣的我还是找不到原因,求大神解答解答。
下面是我的报错的提示:
而我的代码如下:
入口文件 blog.js
var express=require('express'),
mongoose=require('mongoose'),
mongoStore=require('connect-mongo')(express),
blog=module.export=express(),
ejs=require('ejs'),
dbUrl='mongodb://localhost/myblog',
SessionStore=require('session-mongoose')(express),
db;
mongoose.connect(dbUrl);
db=mongoose.connection;
var store=new SessionStore({
url:dbUrl,
interval:120000
})
blog.configure(function(){
blog.set('views',__dirname+'/blog/views');
blog.engine('.html',ejs.__express);
blog.set('view engine','.html');
blog.set("view options",{
layout:false
})
blog.use(express.bodyParser());
blog.use(express.methodOverride());
blog.use(express.cookieParser());
blog.use(express.cookieSession({
key:'user',
secret:'myblog',
store:store,
cookie:{maxAge:1200000}
}));
blog.use(express.static(__dirname+'/public'));
});
db.on('error',function(err){
console.log("link to database error!");
})
db.once('open',function(){
require('./config/route')(blog);
})
blog.listen(4000)
路由文件 route.js
var coverApi=require('../blog/controllers/cover');
module.exports=function(blog){
blog.use(function(req,res,next){
var _user=req.session.user;
blog.locals.user=_user;
next();
})
blog.post('/manage/addCover.html',coverApi.addCover);
}
控制器文件 cover.js
var fs=require('fs'),
path=require('path'),
formidable=require('formidable'),
Article=require('../models/article'),
coverApi={},
util = require('util');
module.exports=coverApi;
coverApi.addCover=function(req,res){
var form = new formidable.IncomingForm({uploadDir:'./sources/'});
form.parse(req, function(err, fields, files) {
if(err) console.log(err);//这里报错了
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
}
coverApi.show=function(req,res){
var id=req.params.id;
res.render('cover',{
id:id
});
}
视图文件 cover.html
<div class="container">
<h1>添加图片</h1>
<form action="/manage/addCover.html" method='post' enctype="multipart/form-data">
<div class="form-group">
<input type="text" class='hidden' value="<%= id%>" name='id'>
<label for="">图片</label>
<input type="file" name='cover'>
<button type='submit' class='btn btn-lg btn-primary'>add</button>
</div>
</form>
</div>
package.json 文件
项目目录结构
现在那个问题还是解决不了,求大神帮忙解答
3 回复
在线等
已解决。明天再说怎么解决的,先睡觉
刚改用 multer 。formidable 还挺硬朗的