node.js相关
- string操作不改变原string,只会返回新string。Buffer只改变原Buffer。
- bin => dup
1
2var dup = new Buffer(bin.length);
bin.copy(dup); - 附件上传
1
2
3
4
5
6
7
8
9
10
11
12const uploadFile = (req, res, next) => {
//生成multiparty对象,并配置上传目标路径
var form = new multiparty.Form({uploadDir: './public/files/'});
//上传完成后处理
form.parse(req, function(err, fields, files) {
var inputFile = files.file[0].path;
console.log('inputFile', inputFile);
res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'});
res.end(inputFile);
//res.end(util.inspect({files: inputFile}));
});
}; - 实现文件下载前端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15router.get('/file/:fileName', function(req, res, next) {
var fileName = req.params.fileName;
var filePath = path.join(__dirname, fileName);
var stats = fs.statSync(filePath);
if(stats.isFile()){
res.set({
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename='+fileName,
'Content-Length': stats.size
});
fs.createReadStream(filePath).pipe(res);
} else {
res.end(404);
}
});<a>
标签加上download属性后就可以下载了
服务器操作
- pm2 list
- pm2 restart 1
- pm2 start
<path/to/app.js>
Yarn
- yarn add [package]
- yarn add [package]@[version]
- yarn add [package]@[tag]
- yarn upgrade [package]
- yarn remove [package]
- yarn install