import deviceMiddleware from 'express-device';
import express from 'express';
import bodyParser from 'body-parser';
import winston from 'winston';

import routerRender from './middleware/routerRender';
import errorRender from './middleware/errorRender';
import staticMiddleware from './middleware/static';

const defaultConfig = {
  logLevel: process.env.NODE_LOGLEVEL || 'info',
  port: process.env.NODE_PORT || 3000,
};

export default function server(userConfig = {}) {
  const app = express();
  const config = { ...defaultConfig, ...userConfig };

  winston.level = config.logLevel;

  app.use('/static', staticMiddleware);

  if (process.env.NODE_ENV === 'production') {
    app.use('/assets', express.static('dist/frontend'));
  }

  app.use(deviceMiddleware.capture());
  app.use(bodyParser.json());
  app.use(bodyParser.json({ type: 'application/json' }));

  if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
    // eslint-disable-next-line global-require
    app.use(require('./middleware/dev').default);
  }

  app.use(routerRender);
  app.use(errorRender);

  app.ready = new Promise((resolve) => {
    app.server = app.listen(config.port, () => {
      winston.log('info', `Started server on port ${config.port}`);
      resolve();
    });
  });

  return app;
}

@@ -356,16 +356,17 @@
 NODE_LOG
+_
 LEVEL %7C%7C

Use nicer name for log level env variable