简体   繁体   English

快递:我可以在单个app.use中使用多个中间件吗?

[英]Express: can I use multiple middleware in a single app.use?

I have a lot of apps full of boilerplate code that looks like this: 我有很多充满样板代码的应用程序,如下所示:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('express-session')({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());

How can I use multiple middleware at once? 如何一次使用多个中间件? So I could turn the above into: 所以我可以将以上内容变成:

// Some file, exporting something that can be used by app.use, that runs multiple middlewares
const bodyParsers = require('body-parsers.js')
const sessions= require('sessions.js')

// Load all bodyparsers
app.use(bodyParsers)

// Load cookies and sessions
app.use(sessions)

You can specify multiple middlewares, see the app.use docs : 您可以指定多个中间件,请参阅app.use docs

An array of combinations of any of the above. 以上任何组合的数组。

You can create a file of all middlewares like - 您可以创建所有中间件的文件,例如-

middlewares.js middlewares.js

module.exports = [
  function(req, res, next){...},
  function(req, res, next){...},
  function(req, res, next){...},
  .
  .
  .
  function(req, res, next){...},
]

and as then simply add it like: 然后像这样简单地添加它:

/*
you can pass any of the below inside app.use()
A middleware function.
A series of middleware functions (separated by commas).
An array of middleware functions.
A combination of all of the above.
*/
app.use(require('./middlewares.js'));

Note - Do this only for those middlewares which will be common for all requests 注意-仅对所有请求通用的中间件执行此操作

I like to use a Router to encapsulate application routes. 我喜欢使用Router来封装应用程序路由。 I prefer them over a list of routes because a Router is like a mini express application. 我更喜欢它们而不是路由列表,因为Router就像一个小型快递应用程序。

You can create a body-parsers router like so: 您可以这样创建一个body-parsers路由器:

const {Router} = require('express');

const router = Router();

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));
router.use(cookieParser());
router.use(require('express-session')({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: false
}));
router.use(passport.initialize());
router.use(passport.session());

module.exports = router;

Then attach it to your main application like any other route: 然后像其他路线一样将其附加到您的主应用程序:

const express = require('express');

const app = express();
app.use(require('./body-parsers'));

Try it like this, in main app.js run just init code: 像这样尝试,在主app.js中仅运行初始化代码:

const bodyParserInit = require('./bodyParserInit');
....
bodyParserInit(app);

And then in bodyParserInit you can do app.use 然后在bodyParserInit中可以执行app.use

module.exports = function (app) {
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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