表达3文件上传:找不到模块'可读流'

时间:2014-07-28 18:25:11

标签: javascript node.js express multipartform-data

我正在关注this tutorial上传文件。我有一个快速3服务器与一些配置。当我解雇所有内容时,express.multipart正在给我一个Error: Cannot find module 'readable-stream'

服务器配置:

app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.multipart({ uploadDir: __dirname + '/uploads', limit: '50mb' }))

路线:

app.post('/upload', function(req, res){
   console.log(req.files.file.name + ' has been uploaded')
   res.send(200)
})

html标记:

<form action='/upload' method='POST' enctype='multipart/form-data'>
    <input type='file' name='file'>
    <input type='submit' class='btn btn-default' value='Upload'>
</form>

我正在使用node v0.10.29npm 1.4.14。处理文件上传的最低快速配置是什么?或者这是我的环境问题?

1 个答案:

答案 0 :(得分:0)

修正了它。

express config:

app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.bodyParser({ uploadDir: path.join(__dirname + '/uploads'), keepExtensions: true }))

路线:

app.post('/upload', function(req, res){
    var fileName = req.files.fileName.name,
        filePath = req.files.fileName.path;

    console.log(fileName + ' added to uploads')

    // Do something with file here:
    // - save metadata to database
    // - upload file to S3 bucket

    // remove file from our server
    fs.unlink(filePath, function(err){
        console.log(fileName + ' has been deleted!')
    })
    res.redirect('/')
}