-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (63 loc) · 1.84 KB
/
index.js
File metadata and controls
70 lines (63 loc) · 1.84 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
/*
eJsServerWrapper
This is a wrapper for express apps.
It allows you to run express apps as a http or https server.
*/
// Dependencies
import express from 'express'
import fs from 'fs'
import http from 'http'
import https from 'https'
/*
@method CreateHTTPSOptions
@param {string} keyPath
@param {string} certPath
@returns {object}
@description Creates an object with the key and cert for https
*/
let CreateHTTPSOptions = (keyPath, certPath) => {
return {
key: fs.readFileSync(keyPath, 'utf8'),
cert: fs.readFileSync(certPath, 'utf8')
}
}
/*
@class eJsServerWrapper
@description This is a wrapper for express apps, allowing you to run express apps as a http or https server.
*/
class eJsServerWrapper {
/*
@constructor
@param {express}
@param {string} keyPath
@param {string} certPath
*/
constructor(app, keyPath, certPath) {
// Create http and https servers
this.httpServer = http.createServer(app)
this.httpsServer = https.createServer(CreateHTTPSOptions(keyPath, certPath), app)
// Misc
this.app = app
}
/*
@method ConnectListeners
@param {number} [httpPort=8080] - The port to listen on for http
@param {number} [httpsPort=8443] - The port to listen on for https
@description Connects the listeners to the servers
*/
ConnectListeners(httpPort = 8080, httpsPort = 8443) {
// Connect listeners
this.httpServer.listen(httpPort)
this.httpsServer.listen(httpsPort)
}
/*
@method DisconnectListeners
@description Disconnects the listeners from the servers
*/
DisconnectListeners() {
// Disconnect listeners
this.httpServer.close()
this.httpsServer.close()
}
}
export { eJsServerWrapper }