我正在尝试使用multer gridFS制作文件上传API,我使用了文档,但缺少以下内容:
这是我的代码:
const config = require("config");
const express = require("express");
const router = express.Router();
const dbURI = config.get("mongoURI");
const multer = require("multer");
const crypto = require("crypto");
const path = require("path");
const GridFsStorage = require("multer-gridfs-storage");
var storage = new GridFsStorage({
url: dbURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString("hex") + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: "uploads"
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
router.post("/upload", upload.single("file"), (req, res) => {
res.json({ msg: "file uploaded successfully" });
});
我收到一个错误TypeError:Router.use()需要一个中间件函数,但是得到了一个对象,该对象基本上告诉我upload.single不是函数,我的错误是什么?
答案 0 :(得分:0)
我发现了错误,我缺少module.exports = router;