简体   繁体   中英

How to separate middleware in feathers?

I use to upload files a multer library with feathers. I try to separate logic from code and I want put upload code not in a index.js but in my case in pdf.js in middleware directory.

Below is my works 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());
};

Here is upload-pdf.js file:

  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);
     }
     });

And pdf.js file:

'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();

  };
};

I would like to combine upload-pdf.js and pdf.js into one file

Not particularly Feathers specific, just as with any other Express application you can put the code into their own modules:

'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());
};

In 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();
  };
};

The NodeJS module system docs are quite helpful to learn how it all fits together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM