This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·69 lines (67 loc) · 1.67 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
var http = require('http'),
fs = require('fs'),
path = require('path'),
mime = require('mime'),
Mincer = require('mincer');
cache = {};
var environment = new Mincer.Environment();
environment.appendPath('scripts');
var jsServer = Mincer.createServer(environment);
function serveStatic(response,absPath) {
// if(cache[absPath]) {
// sendFile(response,absPath,cache[absPath]);
// } else {
fs.exists(absPath,function(exists) {
if(exists) {
fs.readFile(absPath,function(err,data) {
if(err) {
send404(response);
} else {
// cache[absPath] = data;
sendFile(response,absPath,data);
}
});
} else {
send404(response);
}
});
// }
}
function sendFile(response,filePath,fileContents) {
response.writeHead(200,
{"content-type": mime.lookup(path.basename(filePath))}
);
response.end(fileContents);
}
function send404(response) {
response.writeHead(404,{'Content-Type':'text/plain'});
response.end('Error 404: file not found.');
}
var server = http.createServer(function(request,response) {
var filePath = false;
console.log(request.url);
if(request.url==='/build') {
filePath = 'public/index.html';
} else {
if(/.js$/.exec(request.url)!==null) {
// filePath = 'scripts'+request.url;
console.log('js',request.url);
jsServer(request,response);
return;
}
else if(/.css$/.exec(request.url)!==null) {
filePath = 'public'+request.url;
console.log('css',request.url);
}
else {
console.log('other',request.url);
filePath = 'public'+request.url;
}
}
var absPath = './'+filePath;
serveStatic(response,absPath);
});
var port = 8000;
server.listen(port,function() {
console.log('Server listening on port '+port);
});