运行 应用程序时使用 multer 导致错误

Using multer cause error when running app

我有使用 multer 模块的 express 节点应用程序 https://github.com/expressjs/multer

app.js 文件中,我输入了以下内容:

var mulStorage = require("./utils/store"),
    var upload = multer({
        storage: mul.storage,
        dest: 'uploads/'
    });
    app.use(upload.single('file'));

store.js 文件如下所示

var multer = require('multer');

var stor = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/')
    },
    filename: function (req, file, cb) {
        var filename = file.originalname;
        var fileExtension = filename.split(".")[1];
        cb(null, Date.now() + "." + fileExtension);
    }
})
module.exports = {
    stor: stor
}

当我 运行 使用 postman 请求时,出现以下错误:

Error: ENOENT: no such file or directory, open 'c:\Users\c45669\WebstormProjects\App\uploads54935327214.zip'
   at Error (native)

如果文件夹不存在,为什么multer不创建??

如果我在根目录下创建上传文件夹手动这是可行的...

顺便说一句, 当我更改以下内容并删除 storage: mul.storage, 这是可行的,但我需要存储空间来确定文件名...

    var upload = multer({
        //storage: mul.storage,
        dest: 'uploads/'
    });

即使我从 multer 对象中删除 属性 dest 并只保留存储,我也会遇到同样的错误...

Why multer doesn't create the folder if it doesn't exist??

这在文档中 (link) :

Note: You are responsible for creating the directory when providing destination as a function. When passing a string, multer will make sure that the directory is created for you.

我不知道作者为什么做出这个决定,但正如你所见,这不是一个错误。

您可以使用 fs 模块创建目录。

如果有人接受我解决的错误,我会遇到同样的问题

var storage = multer.diskStorage({
 destination: function (req, file, cb) {
 cb(null, 'this is not just /uploads do it like /var/www/....') thank you!!
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage });