-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·52 lines (41 loc) · 1.56 KB
/
server.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
#!/usr/local/bin/nodemon --use-strict
// This shebang is the one I use for development, it should not be recomended for deployment
// I use 'use strict' because it enforces good programming habits.
// As it only applies to the file it is, I run the server with the --use-strict flag, which applies it to all files run.
'use strict';
// The first thing we import is logs, so that if something goes wrong right away, we know.
const log = require("./js/log.js");
// The configuration for the server
const config = require("./config.json").server;
// Our actual app. The server just acts as a connection mechanism
var app = require("./app.js");
// There are two servers we can make, tcp or tls. tls is secure, tcp isn't
// If the config is set up for tls, use it
if (config.secure){
// First, get the certs.
// fs is a a library to read local files
const fs = require('fs');
// Read our cert and key from the file
const options = {
key: fs.readFileSync(config.ssl.key),
cert: fs.readFileSync(config.ssl.cert)
};
// tls is the secure library
const tls = require('tls');
// Create a server with our cert and key
var server = tls.createServer(options, app);
}
// If they don't, use tcp
else {
// Import the "net" library, and make a server with it
const net = require('net');
var server = net.createServer(app);
}
// Listening for any problems with the server
server.on('error', function(error) {
log.warn(error);
});
// Tell the server to listen on specified port
server.listen(config.port, function() {
log.server("Server listening at localhost on port " + config.port);
});