-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (61 loc) · 1.61 KB
/
Copy pathserver.js
File metadata and controls
71 lines (61 loc) · 1.61 KB
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
var p = process;
"use strict";
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var mimeTypes = {
"html": "text/html",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"
};
var nombre = p.argv[2];
http.createServer(staticServer).listen(3000);
console.log('escuchando... '+ nombre+' Mira en http://localhost:3000 ');
function staticServer(req, res) {
if(req.url.indexOf('jspm_packages')<0 && req.url.indexOf('refs')<0) console.log("req.url: " + req.url);
var urlParseada = url.parse(req.url);
var pathname = urlParseada.pathname;
fileServer(res, pathname);
}
function fileServer(res, pathname) {
if(pathname==="/") pathname = "/index.html";
var filename = path.join(p.cwd(),nombre, pathname);
var extension = path.extname(filename).split(".")[1];
if (!extension) {
extension = "html";
filename += "." + extension;
}
fs.exists(filename, ifExists);
function ifExists(exists) {
if (exists) {
sendFile(res, extension, filename);
} else {
console.log("no encuentro: " + filename);
notFound(res);
}
}
}
function sendFile(res, extension, filename) {
var mimeType = mimeTypes[extension];
res.writeHead(200, {
'Content-Type': mimeType
});
fs.createReadStream(filename).pipe(res);
}
function notFound(res) {
res.writeHead(404, {
'Content-Type': 'text/html'
});
res.write("<html>");
res.write("<head>");
res.write("<meta charset='utf-8'>");
res.write("<title>Front Edge Server</title>");
res.write("</head>");
res.write("<body>");
res.write('<h1>404</h1> Nada por aquí :-(');
res.write("</body>");
res.write("</html>");
res.end();
}