-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.58 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
// server.js
const http = require('http');
const fileSystem = require('fs');
const path = require('path');
const hostname = 'localhost';
const host_port = 8000;
const client_port = 8001;
const client_port2 = 8002;
const host_server = http.createServer((req, res) => {
serveAsset('host', req.url, res)
}).listen(host_port, hostname, () => {
console.log(`Server running at http://${hostname}:${host_port}/`);
});;
const client_server = http.createServer((req, res) => {
serveAsset('client', req.url, res)
}).listen(client_port, hostname, () => {
console.log(`Client running at http://${hostname}:${client_port}/`);
});
const client2_server = http.createServer((req, res) => {
serveAsset('client2', req.url, res)
}).listen(client_port2, hostname, () => {
console.log(`Client2 running at http://${hostname}:${client_port2}/`);
});
/*
const sharepoint_server = http.createServer((req, res) => {
serveAsset('sharePoint', req.url, res)
}).listen(client_port2, hostname, () => {
console.log(`SharePoint running at http://${hostname}:${client_port2}/`);
});
*/
function serveAsset(rootPath, url, res) {
// default root route to index.html in the folder
if (url === '/') url = 'index.html';
const filePath = path.join(__dirname, rootPath, url)
const readStream = fileSystem.createReadStream(filePath)
.on('error', function() {
res.statusCode = 404;
res.end();
});
if (/^.*\.js$/.test(url)) {
res.setHeader('Content-Type', 'text/javascript');
} else {
res.setHeader('Content-Type', 'text/html');
}
readStream.pipe(res);
}