-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (77 loc) · 2.24 KB
/
Copy pathindex.js
File metadata and controls
91 lines (77 loc) · 2.24 KB
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
84
85
86
87
88
89
90
91
/* eslint no-console: [0] */
'use strict'
const lib = require('./lib')
const _ = require('lodash')
const ServerTrailpack = require('trailpack/server')
/**
* Express Trailpack
*
* @class Express
* @see {@link http://trailsjs.io/doc/trailpack}
*
* Bind application routes to Express.js (from trailpack-router)
*/
module.exports = class Express extends ServerTrailpack {
/**
* Ensure that config/web is valid, and that no other competing web
* server trailpacks are installed (e.g. express)
*/
async validate () {
if (_.includes(_.keys(this.app.config.get('main.packs')), 'hapi', 'koa', 'koa2', 'restify')) {
return Promise.reject(
new Error('There is another web services trailpack installed that conflicts with trailpack-express!'))
}
if (!this.app.config.get('web.express')) {
return Promise.reject(
new Error('config.web.express is absent, please npm install your express version (4 or 5) and uncomment the line under config.web.express'))
}
console.log('express', typeof this.app.config.get('web.express'))
return Promise.all([
lib.Validator.validateWebConfig(this.app.config.get('web'))
])
}
configure () {
this.app.config.set('web.server', 'express')
}
/**
* Start Express Server
*/
async initialize () {
this.server = lib.Server.createServer(this.app)
return Promise.all([
lib.Server.registerMiddlewares(this.app, this.server),
lib.Server.registerViews(this.app, this.server)
])
.then(() => {
return lib.Server.start(this.app, this.server)
})
.then(() => {
this.app.emit('webserver:http:ready', lib.Server.nativeServer)
})
}
unload() {
if (lib.Server.nativeServer.listening) {
this.closeServer()
}
else {
this.app.on('webserver:http:ready', () => this.closeServer())
}
}
closeServer() {
if (lib.Server.nativeServer === null) {
return
}
let servers = lib.Server.nativeServer
if (!_.isArray(lib.Server.nativeServer)) {
servers = [servers]
}
servers.forEach(server => server.close())
}
constructor(app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}