UNPKG

3.41 kBJavaScriptView Raw
1/**
2 * Module Dependencies
3 *
4 * @ignore
5 */
6var bodyParser = require('body-parser')
7var contentLength = require('express-content-length-validator')
8var cors = require('cors')
9var express = require('express')
10var helmet = require('helmet')
11var hpp = require('hpp')
12var logger = require('structure-logger')
13var path = require('path')
14var Router = require('structure-router')
15var serveStatic = require('serve-static')
16var WebsocketServer = require('structure-websocket-server')
17
18function removePoweredBy(req, res, next) {
19 res.removeHeader('X-Powered-By')
20 next()
21}
22
23/**
24 * Server Class
25 *
26 * @public
27 * @class Server
28 */
29class Server {
30
31 /**
32 * Server constructor
33 *
34 * @public
35 * @constructor
36 * @param {Object} options - Options
37 */
38 constructor(options = {}) {
39
40 var defaults = {
41 cursors: options.cursors || [],
42 db: true,
43 drain: true,
44 sockets: true
45 }
46
47 this.options = Object.assign({}, defaults, options)
48
49 this.server = express()
50
51 /*
52 TODO: Add whitelist
53 */
54 this.server.use(cors())
55
56 /*if(process.env.NODE_ENV != 'test') {
57 this.server.use(require('express-status-monitor')())
58 }*/
59 this.server.use(serveStatic(path.join(__dirname, '../public')))
60 this.server.use(bodyParser.urlencoded({extended: true}))
61 this.server.use(bodyParser.json({strict: false}))
62 //this.server.use(removePoweredBy)
63 this.server.use(helmet())
64 this.server.use(hpp())
65 //this.server.use(contentLength.validateMax({max: process.env.MAX_CONTENT_LENGTH, status: 400, message: 'stop it!'}))
66
67 if(process.env.NODE_ENV != 'test') {
68 this.server.use(this.logRequestInfo)
69 }
70
71 this.router = options.router || new Router({
72 routes: options.routes
73 })
74
75 this.router.start(this.server)
76
77 let routes = this.router.routesList
78
79 // For debuggin
80 this.routesList = routes
81
82 this.server.get(`/api/${process.env.API_VERSION}`, function routesListHandler(req, res) {
83
84 res.json({
85 routes
86 })
87
88 })
89
90 }
91
92 closeCursors() {
93 var cursors = require('./cursors')
94
95 for(let i = 0, l = cursors.length; i < l; i++) {
96 var cursor = cursors[i]
97 cursor.close()
98 }
99
100 cursors = []
101
102 }
103
104 /**
105 * Log each HTTP request's method and url
106 *
107 * @private
108 * @param {Object} req - Express req
109 * @param {Object} res - Express res
110 * @param {Function} next - Express next
111 */
112 logRequestInfo(req, res, next) {
113
114 logger.info(req.method, req.originalUrl)
115 next()
116
117 }
118
119 /**
120 * Start the HTTP Server
121 *
122 * @public
123 */
124 start() {
125
126 this.server = this.server.listen(this.options.port || process.env.EXPRESS_PORT)
127
128 if(this.options.sockets) {
129 this.wss = new WebsocketServer(this.server)
130 }
131
132 if(this.options.db) {
133 this.cursors = []
134 }
135
136 logger.debug('Structure API started at:', `http://localhost:${process.env.EXPRESS_PORT}`)
137
138 }
139
140 /**
141 * Stop the HTTP Server
142 *
143 * @public
144 */
145 stop() {
146
147 if(this.options.db) {
148 var r = require('../lib/database/driver')
149 if(this.options.drain) r.getPoolMaster().drain()
150 this.closeCursors()
151 }
152
153 if(this.options.sockets) {
154 this.wss.stop()
155 }
156
157 this.server.close()
158
159 }
160
161 /**
162 * Add Express middleware to the Express server object
163 *
164 * @public
165 */
166 use() {
167
168 this.server.use.apply(this.server, arguments)
169
170 }
171
172}
173
174module.exports = Server