-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
index.js
55 lines (44 loc) · 2.18 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
let http = require('http');
let fs = require('fs');
let path = require('path');
/* note, mysql must be installed (npm install mysql) and mysql server running on localhost or elsewhere*/
let { API, database } = require('./module/api/api.js');
database.create();
process.env.node_env = "localhost";
// replace xx.xx.xx.xxx with your own remote IP address or localhost (127.0.0.1)
const ip = '23.239.21.207';
const port = process.env.node_env === 'production' ? 80 : 3000;
http.createServer(function(request, response) {
console.log('request ', request.url);
let filename = '.' + request.url;
if (filename == './')
filename = './index.html';
let extension = String(path.extname(filename)).toLowerCase();
let mime = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpg', '.gif': 'image/gif', }
let contentType = mime[extension] || 'application/octet-stream';
fs.readFile(filename, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
if (API.catchAPIrequest( request.url ))
API.exec(request, response);
else
fs.readFile('./404.html', function (error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
} else {
response.writeHead(500)
response.end('Server error: ' + error.code + ' ..\n');
response.end();
}
} else {
console.log("API request detecting...");
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}).listen(port, ip);
process.on('exit', function () { database.connection.end(); console.log('process.exit'); });
process.on('SIGINT', function () { console.log('Ctrl-C...'); database.connection.end(); process.exit(2) });
process.on('uncaughtException', function(e) { console.log(e.stack); database.connection.end(); process.exit(99); });
console.log('Server running at http://' + ip + ':' + port + '/');