All files / server/plugins/express index.js

82.35% Statements 28/34
50% Branches 6/12
90% Functions 9/10
86.66% Lines 26/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8427x 27x 27x   27x                                                         102x 102x     102x 414x 414x 414x         102x         102x 102x 102x 102x         102x   102x   102x           102x       102x 102x 102x 102x   102x 8x   102x      
const express = require('express');
const hbs = require('hbs');
const path = require('path');
 
module.exports = {
  name: 'express',
  options: {
    // See these in-depth in https://expressjs.com/en/api.html#app.set
    'case sensitive routing': {},
    'env': {
      inherit: 'env'
    },
    'etag': {},
    'jsonp callback name': {},
    'json replacer': {},
    'json spaces': {},
    'query parser': {},
    'strict routing': {},
    'subdomain offset': {},
    'trust proxy': {},
    'views': {
      default: 'views',
      inherit: true,
      type: String,
      folder: true
    },
    'view cache': {},
    'view engine': {
      inherit: 'engine'
    },
    'x-powered-by': {}
  },
  init: async ctx => {
    ctx.express = express;
    ctx.app = ctx.express();
 
    // Go through all of the options and set the right ones
    for (let key in ctx.options.express) {
      let value = ctx.options.express[key];
      Eif (typeof value !== 'undefined') {
        ctx.app.set(key, value);
      }
    }
 
    // Add the views into the core
    Iif (path.resolve(ctx.options.views) === path.resolve(process.cwd())) {
      throw new Error(
        'The "views" option should point to a subfolder of the project and not the root of it'
      );
    }
    await new Promise(function(resolve, reject) {
      hbs.registerPartials(ctx.options.views, function(err) {
        Iif (err) reject(err);
        resolve();
      });
    });
 
    // Accept HTML as a render extension
    ctx.app.engine('html', hbs.__express);
 
    Eif (ctx.options.engine) {
      // If it's an object, expect a { engine: { engineName: engineFN } }
      Iif (typeof ctx.options.engine === 'object') {
        for (let name in ctx.options.engine) {
          ctx.app.engine(name, ctx.options.engine[name]);
          ctx.app.set('view engine', name);
        }
      } else {  // Simple case like { engine: 'pug' }
        ctx.app.set('view engine', ctx.options.engine);
      }
    }
  },
  listen: ctx => new Promise((resolve, reject) => {
    ctx.server = ctx.app.listen(ctx.options.port, () => {
      ctx.log.debug(`Server started on http://localhost:${ctx.options.port}/`);
      resolve();
    });
    ctx.close = () => new Promise((res, rej) => {
      ctx.server.close(err => err ? rej(err) : res());
    });
    ctx.server.on('error', err => reject(err));
  })
};