如何在调用post方法后更改用multer上传的文件名

时间:2017-10-23 18:28:23

标签: javascript node.js express multer

我想使用带有uniqe名字的multer来保存图片。文件名的模式为file.orginalName + req.body.userName

我的代码:

const express = require('express');
const router = express.Router();
const multer  = require('multer');
const bcrypt  = require('bcrypt');
const passport = require('passport');

let User = require('../models/user');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/avatars/')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
});

const upload = multer({ storage: storage })


router.post('/register', upload.single('avatar'), (req, res) => {

  ////here I want to change the uploaded avatar name then save to DB
});

module.exports = router;

如何更改代码以重命名post方法中的图片名称?

1 个答案:

答案 0 :(得分:1)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}