-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
62 lines (57 loc) · 1.69 KB
/
app.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
const http = require('http');
const fs = require('fs');
const url = require('url');
const path = require('path');
const hostname = '127.0.0.1';
const port = 3000;
const mimetypes = {
'html': 'text/html',
'css': 'text/css',
'js': 'text/javascript',
'png': 'image/png',
'jpeg': 'image/jpeg',
'jpg': 'image/jpg'
};
fs.readFile('index.html', (err, html) => {
if (err) {
throw err;
}
const server = http.createServer((req, res) => {
const myuri = url.parse(req.url).pathname;
const filename = path.join(process.cwd(), unescape(myuri));
// console.log('File you are looking for is:' + filename);
let loadFile;
try {
loadFile = fs.lstatSync(filename);
} catch (error) {
res.writeHead(404, {
"Content-Type": 'text/plain'
});
res.write('404 Internal Error');
res.end();
return;
}
if (loadFile.isFile()) {
let mimeType = mimetypes[path.extname(filename).split('.').reverse()[0]];
res.writeHead(200, {
"Content-Type": mimeType
});
let filestream = fs.createReadStream(filename);
filestream.pipe(res);
} else if (loadFile.isDirectory()) {
res.writeHead(302, {
'Location': 'index.html'
});
res.end();
} else {
res.writeHead(500, {
"Content-Type": 'text/plain'
});
res.write('500 Internal Error');
res.end();
}
});
server.listen(port, hostname, () => {
console.log('Server started on port '+port);
});
})