nodejs快速实现文件上传和下载

//index.js
 
var fs = require('fs')
var express = require('express')
var multer = require('multer')
const path = require('path');
 
 var app = express();
 var upload = multer({dest:'upload/'});
 
//多文件上传 (限定上传文件个数)(没有修改后缀)
app.post('/upload-multi',upload.array('myfile',2),function(req,res,next){
  res.send("2 done");
})
//单文件上传获取信息
app.post('/upload-single',upload.single('myfile'),function(req,res,next){
  var file=req.file;
   console.log("名称:%s",file.originalname);
   console.log("mime:%s",file.mimetype);
  res.send("done");
  fs.renameSync('./upload/'+file.filename,'./upload/'+file.originalname);
 
})
 

app.get('/download', function(req, res){
 var file = __dirname + '/upload/'+req.query.file;
 res.download(file); 
});
app.get('/',function(req,res,next){
  res.sendFile(__dirname+"/index.html");
})
 
app.listen(3000);

运行需要安装一些模块, 可以快速实现文件上传和下载