0
import express from 'express'
import bodyParser from 'body-parser'
import api from 'routes'
import timeout from 'connect-timeout'
import haltOnTimeout from 'middlewares/haltOnTimeout'
import compression from 'compression'
import helmet from 'helmet'
import path from 'path'

const app = express()

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(timeout(3000))
app.use(haltOnTimeout)

app.use(compression()) // production
app.use(helmet()) // production

app.use('/api', api)

app.use(express.static(path.resolve(`${__dirname}/../../client/build`))) // production

export default app

I have my app.js like this.

now I'm going to deploy my app on aws.

In order to do this,

I want to divide my middlewares into array and execute it based on environment.

For example,

const middlewares = [
  bodyParser.json(),
  bodyParser.urlencoded({ extended: false }),
  timeout(3000),
  ...
]

const productionMiddlewares = [
  compression(),
  helmet()
  ...
]

Any workaround for this?

Gompro
  • 2,247
  • 1
  • 16
  • 26

3 Answers3

1

Standard way

app.configure('development', function() {
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function() {
  app.use(express.errorHandler()); 
});

Simple way

var middlewares= process.env.NODE_ENV == 'production' ? [compression, ...] : [some-other];
middlewares.forEach((middleware) => app.use(middleware));
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
0

You can go through the required array and register the middleware. For example:

for (var m in (app.get('env') === 'production' ? productionMiddlewares : middlewares)) {
    app.use(m)
}
stdob--
  • 28,222
  • 5
  • 58
  • 73
0

My answare is inspired by sailsjs

You can have two config file and a structure like this

root
  |-app.js
  |-package.json
  |-bootstrapper.js
  |-config
     |- middleware.js

Basically, you will write your middleware divided by environment in the middleware.js file, the bootstrapper.js file will hold the logic mount the middleware into the express application, the bootstrapper can use the NODE_ENV variable to choose which middleware set to mount. The bootstrapper will be then called by your app.js during startup.


//app.js
import express from 'express'
import bodyParser from 'body-parser'
import api from 'routes' 
import timeout from 'connect-timeout'
import haltOnTimeout from 'middlewares/haltOnTimeout'
import compression from 'compression'
import helmet from 'helmet'
import path from 'path'
import bootstrapper from 'bootstrap'

const app = express()

bootstrap.bootMiddleware(app)

app.use('/api', api)
app.use(express.static(path.resolve(`${__dirname}/../../client/build`))) // production

export default app

In your middleware.js file you define the middlewares and the order in which you have to mount them, you will have different file based on your environment.

// */middleware.js
exports.module = {
   'dev_middlewares' : [
      'bodypParserJson',
       // other middlewares
      'helmet'
   ],
   'prod_middlewares' : [
      'bodypParserJson',
       // other middlewares
      'helmet'
   ],
   'helmet' : helmet(),
   'bodypParserJson': bodyParser.json()
}

The bootstrapper then will have the function to mount the appropriate middlewares

// bootstrapper
function loadMiddleware(app, list, middlewareConf){
  _.foreach(middlewareName => {
     app.use(middlewareConf[middlewareName])
  })
}

function bootMiddleware(app){
   let env = process.env.NODE_ENV || 'dev';
   var middlewareConf = require('./config/middleware')
   if(env==='dev') {
      loadMiddleware(app, dev_middleware, middlewareConf)
   } else {
      loadMiddleware(app, prod_middleware, middlewareConf)
   }
}
export bootMiddleware
Simone Pontiggia
  • 204
  • 2
  • 10