-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·92 lines (83 loc) · 2.25 KB
/
index.js
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
92
/* eslint-disable no-console */
const http = require('http');
const faviconRequest = require('./favicon-request');
/**
* Define the sample application.
*/
class FaviconApp {
/**
* Set up server IP address and port # using env variables/defaults.
*/
setupVariables() {
this.port = process.env.PORT || 8080;
}
/**
* terminator === the termination handler
* Terminate server on receipt of the specified signal.
* @param {string} sig Signal to terminate on.
*/
terminator(sig) {
if (typeof sig === 'string') {
console.log('%s: Received %s - terminating favicon app ...', new Date(Date.now()), sig);
process.exit(1);
}
if (this.server) {
this.server.close();
this.server = null;
}
console.log('%s: Node server stopped.', new Date(Date.now()));
}
/**
* Setup termination handlers (for exit and a list of signals).
*/
setupTerminationHandlers() {
process.on('exit', () => {
this.terminator();
});
// Removed 'SIGPIPE' from the list - bugz 852598.
[
'SIGHUP',
'SIGINT',
'SIGQUIT',
'SIGILL',
'SIGTRAP',
'SIGABRT',
'SIGBUS',
'SIGFPE',
'SIGUSR1',
'SIGSEGV',
'SIGUSR2',
'SIGTERM'
].forEach((element) => {
process.on(element, () => {
this.terminator(element);
});
});
}
/**
* Initialize the server (express) and create the routes and register
* the handlers.
*/
initializeServer() {
this.server = http.createServer(faviconRequest);
}
/**
* Initializes the sample application.
*/
initialize() {
this.setupVariables();
this.setupTerminationHandlers();
this.initializeServer();
}
/**
* Start the server (starts up the sample application).
*/
start() {
this.server.listen(this.port, () => {
console.log('%s: Node server started on port %d ...', new Date(Date.now()), this.port);
});
}
}
const app = new FaviconApp();
app.initialize();
app.start();