我用上传文件带有羽毛的multer库。我尝试将逻辑与代码分开,我希望将上传代码放在index.js中,但在中间件目录中的pdf.js中。
以下是我的作品index.js:
'use strict';
const pdf = require('./pdf');
const record = require('./record');
const records = require('./records');
const handler = require('feathers-errors/handler');
const notFound = require('./not-found-handler');
const logger = require('./logger');
const uploadPdf = require('./upload-pdf');
module.exports = function() {
// Add your custom middleware here. Remember, that
// just like Express the order matters, so error
// handling middleware should go last.
const app = this;
app.use('/rekord/:id.html', record(app));
app.use('/rekordy.html', records(app));
app.use('/pdf/:id', uploadPdf.single('file'), pdf(app));
app.use(notFound());
app.use(logger(app));
app.use(handler());
};
这是upload-pdf.js文件:
var multer = require('multer')
var storagePdf = multer.diskStorage({
destination: 'public/pdf',
filename: function (req, file, cb) {
var id = req.params.id
cb(null, id+'.pdf')
}
});
module.exports = multer({
storage: storagePdf,
fileFilter: function (req, file, cb) {
if (file.mimetype !== 'application/pdf') {
return cb(null, false, new Error('I don\'t have a clue!'));
}
cb(null, true);
}
});
pdf.js文件:
'use strict';
module.exports = function(app) {
return function(req, res, next) {
if (req.file) {
return res.end('Thank you for the file');
}
return res.end('false');
next();
};
};
我想将upload-pdf.js和pdf.js合并到一个文件中
答案 0 :(得分:0)
不是特别针对Feathers,就像任何其他Express应用程序一样,您可以将代码放入自己的模块中:
'use strict';
const pdf = require('./pdf');
const record = require('./record');
const records = require('./records');
const handler = require('feathers-errors/handler');
const notFound = require('./not-found-handler');
const logger = require('./logger');
module.exports = function() {
// Add your custom middleware here. Remember, that
// just like Express the order matters, so error
// handling middleware should go last.
const app = this;
app.use('/rekord/:id.html', record(app));
app.use('/rekordy.html', records(app));
app.use('/pdf/:id', pdf.upload.single('file'), pdf.process(app));
app.use(notFound());
app.use(logger(app));
app.use(handler());
};
在pdf.js
:
'use strict';
var multer = require('multer')
var storagePdf = multer.diskStorage({
destination: 'public/pdf',
filename: function(req, file, cb) {
var id = req.params.id
cb(null, id + '.pdf')
}
});
exports.upload = multer({
storage: storagePdf,
fileFilter: function(req, file, cb) {
if (file.mimetype !== 'application/pdf') {
return cb(null, false, new Error('I don\'t have a clue!'));
}
cb(null, true);
}
});
exports.process = function(app) {
return function(req, res, next) {
if (req.file) {
return res.end('Thank you for the file');
}
return res.end('false');
next();
};
};
NodeJS module system docs非常有助于了解它们如何组合在一起。