-
Notifications
You must be signed in to change notification settings - Fork 4
/
webserver.js
87 lines (73 loc) · 2.06 KB
/
webserver.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
// A very basic web server in node.js
// Script from: Node.js for Front-End Developers by Garann Means (p. 9-10)
//dependencies: npm install node-cmd
//dependencies: npm install http
//dependencies: npm install path
//dependencies: npm install fs
var cmd=require('node-cmd');
var http = require("http");
var path = require("path");
var fs = require("fs");
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var serverUrl = config.serverip;
var port = config.serverport;
var checkMimeType = true;
console.log("Starting web server at " + serverUrl + ":" + port);
http.createServer( function(req, res) {
var now = new Date();
if(req.url == "/"){
req.url="/index.html";
}
if(req.url == "/data.json"){
cmd.run('bash getdata.sh');
req.url="/data.json";
}
console.log("Requesting "+ now +" url: " + req.url);
var filename = req.url || "index.html";
var ext = path.extname(filename);
var localPath = __dirname;
var validExtensions = {
".html" : "text/html",
".js": "application/javascript",
".css": "text/css",
".jpg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".json": "application/json"
};
var validMimeType = true;
var mimeType = validExtensions[ext];
if (checkMimeType) {
validMimeType = validExtensions[ext] != undefined;
}
if (validMimeType) {
localPath += filename;
fs.exists(localPath, function(exists) {
if(exists) {
console.log("Serving file: " + localPath);
getFile(localPath, res, mimeType);
} else {
console.log("File not found: " + localPath);
res.writeHead(404);
res.end();
}
});
} else {
console.log("Invalid file extension detected: " + ext + " (" + filename + ")")
}
}).listen(port, serverUrl);
function getFile(localPath, res, mimeType) {
fs.readFile(localPath, function(err, contents) {
if(!err) {
res.setHeader("Content-Length", contents.length);
if (mimeType != undefined) {
res.setHeader("Content-Type", mimeType);
}
res.statusCode = 200;
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}