-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
113 lines (96 loc) · 3.75 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var http = require('http');
var fs = require('fs');
var path = require('path');
var os = require('os');
const port = 8080
function getIpAddress(ipVersion='IPv4') {
let ifaces = os.networkInterfaces();
let ips = Object.keys(ifaces)
.map(ifname => {
return ifaces[ifname]
.filter(iface => iface.family === ipVersion && iface.address !== '127.0.0.1' && iface.address !== '::1' && iface.scopeid != 0)
})
.filter(iface => iface.length > 0)
.reduce((acc, val) => acc.concat(val), [])
.map(iface => iface.address)
return ips.length > 0 ? ips[0] : 'localhost'
// return Object.keys(ifaces)
// .map((ifname) => {
// var alias = 0;
// console.log(ifaces)
// return ifaces[ifname]
// .map((iface) => {
// if ('IPv4' !== iface.family || iface.internal !== false) {
// // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
// return;
// }
// if (alias >= 1) {
// // this single interface has multiple ipv4 addresses
// return { name: ifname, alias, ip: iface.address }
// } else {
// // this interface has only one ipv4 adress
// return { name: ifname, ip: iface.address }
// }
// ++alias;
// })
// .filter(ifname => ifname != undefined)
// })
// .filter(ifname => ifname != undefined)
}
http.createServer(function (request, response) {
console.log('request starting...\n');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
}
// Website you wish to allow to connect
response.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE'); /* OPTIONS,*/
// Request headers you wish to allow
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
response.setHeader('Access-Control-Allow-Credentials', true);
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
fs.readFile('./404.html', function (error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}).listen(port, getIpAddress());
console.log(`Server running at http://[${getIpAddress()}]:${port} ...`);