如何将中间件移动到服务层
How to move middleware to Service Layer
我正在尝试将中间件功能移动到服务层,但 运行 出现错误。我什至不确定我是否遵循正确的分层。
错误:
** Route.post() requires a callback function but got a [object Object]**
file.service.ts
require('rootpath')()
const db = require('helpers/db.ts')
const mongoose = require('mongoose')
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
const myCrypto = require('crypto')
const path = require('path')
var gfs = Grid(mongoose.connection, mongoose.mongo);
gfs.collection('uploads');
var storage = new GridFsStorage({
//url: mongoose.connection.client.s.url,
//options: options,
db: mongoose.connection,
file: (req, file) => {
return new Promise((resolve, reject) => {
myCrypto.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 });
module.exports = {
upload
}
file.controller.ts
require('rootpath')()
const express = require('express')
const router = express.Router()
const fileService = require('routes/_shared/file.service.ts')
var logger = function(req, res, next)
{
console.log("File Controller");
next();
}
router.use(logger);
router.post('/upload', fileService.upload, fileUpload);
module.exports = router;
function fileUpload(req, res) {
res.send({ file: req.file })
}
请仔细检查 multer
usage 文档。
您将 fileService.upload
作为第二个参数传递给 router.post
方法,并且 fileService.upload
是一个对象而不是函数(错误消息指向)。
相反,您应该传递 fileService.upload.single('avatar')
或使用其他 upload
方法,例如 array
、fields
、any
、none
。
我正在尝试将中间件功能移动到服务层,但 运行 出现错误。我什至不确定我是否遵循正确的分层。
错误:
** Route.post() requires a callback function but got a [object Object]**
file.service.ts
require('rootpath')()
const db = require('helpers/db.ts')
const mongoose = require('mongoose')
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
const myCrypto = require('crypto')
const path = require('path')
var gfs = Grid(mongoose.connection, mongoose.mongo);
gfs.collection('uploads');
var storage = new GridFsStorage({
//url: mongoose.connection.client.s.url,
//options: options,
db: mongoose.connection,
file: (req, file) => {
return new Promise((resolve, reject) => {
myCrypto.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 });
module.exports = {
upload
}
file.controller.ts
require('rootpath')()
const express = require('express')
const router = express.Router()
const fileService = require('routes/_shared/file.service.ts')
var logger = function(req, res, next)
{
console.log("File Controller");
next();
}
router.use(logger);
router.post('/upload', fileService.upload, fileUpload);
module.exports = router;
function fileUpload(req, res) {
res.send({ file: req.file })
}
请仔细检查 multer
usage 文档。
您将 fileService.upload
作为第二个参数传递给 router.post
方法,并且 fileService.upload
是一个对象而不是函数(错误消息指向)。
相反,您应该传递 fileService.upload.single('avatar')
或使用其他 upload
方法,例如 array
、fields
、any
、none
。