UNPKG

1.75 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Base');
7
8module.exports = class ExpressEngine extends Base {
9
10 constructor (config) {
11 super(config);
12 this._express = express();
13 this._express.disable('x-powered-by');
14 this._handlers = []; // for deferred assign
15 }
16
17 getExpress () {
18 return this._express;
19 }
20
21 add (method, ...args) {
22 this._handlers.push({method, args});
23 }
24
25 addChild (mountPath, express) {
26 this.add('use', mountPath, express._express);
27 }
28
29 addViewEngine (params) {
30 if (params) {
31 this.add('engine', params.extension, params.engine);
32 this.add('set', 'view engine', params.extension);
33 this.add('set', 'views', params.views || '/');
34 }
35 }
36
37 attach (method, ...args) {
38 this._express[method](...args);
39 this.log('trace', method, args[0]);
40 }
41
42 attachStatic (route, dir, options) {
43 this.attach('use', route, express.static(dir, options));
44 this.log('trace', 'static', dir);
45 }
46
47 attachChild (mountPath, express) {
48 this.attach('use', mountPath, express._express);
49 }
50
51 attachHandlers () {
52 for (const item of this._handlers) {
53 this.attach(item.method, ...item.args);
54 }
55 }
56
57 createServer () {
58 return http.createServer(this._express);
59 }
60
61 log () {
62 CommonHelper.log(this.module, this.constructor.name, ...arguments);
63 }
64};
65
66const express = require('express');
67const http = require('http');
68const CommonHelper = require('../helper/CommonHelper');
\No newline at end of file