简体   繁体   中英

express 3 file upload: Cannot find module 'readable-stream'

I am following this tutorial on uploading a file. I have an express 3 server with some config. The express.multipart is giving me a Error: Cannot find module 'readable-stream' when I fire everything up

server config:

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' }))

route:

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

html markup:

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

I am using node v0.10.29 and npm 1.4.14 . What's the minimum express config for handling a file upload? Or is this a problem with my environment?

Fixed it.

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 }))

route:

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('/')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM