-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_demo.js
executable file
·98 lines (85 loc) · 2.94 KB
/
server_demo.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
88
89
90
91
92
93
94
95
96
97
98
const http = require('http');
const path = require('path');
const fs = require('fs');
const res = require('express/lib/response');
const server = http.createServer((req, res) => {
let done = 0;
var mime = {
html: 'text/html',
txt: 'text/plain',
css: 'text/css',
gif: 'image/gif',
jpg: 'image/jpeg',
png: 'image/png',
svg: 'image/svg+xml',
js: 'application/javascript'};
var dir = path.join(__dirname, 'public');
var reqpath = req.url.toString().split('?')[0];
console.log(`reqpath= ${reqpath}`);
var file = path.join(dir, reqpath.replace(/\/$/, '/index.html'));
var type = mime[path.extname(file).slice(1)] || 'text/html'; // Should this default to 'text/plain'?
// Build file path
let rpath = req.url.slice(1); // Remove leading /
let filePath = path.join(__dirname, 'public', req.url === '/' ?
'index.html' : rpath + '.html');
// Extension of the file
let extname = path.extname(filePath);
// Handle css file
if ( !done ) {
if(req.url.indexOf('.css') != -1){ //req.url has the pathname, check if it conatins '.css'
fs.readFile(__dirname + '/public/css/style.css', function (err, data) {
if (err) throw err;
res.writeHead(200, {'Content-Type': type});
res.write(data);
res.end();
done = 1;
});
}
}
// Handle jpg file
if ( !done ) {
if (/.(jpg)$/.test(reqpath)) { // test if req.url has jpg extension
var s = fs.createReadStream(reqpath);
s.on('open', function() {
res.setHeader('Content-Type', 'text/html');
s.pipe(res);
});
s.on('error', function() {
res.setHeader('Content-Type', type);
res.statusCode= 404;
res.end('Not found');
});
done = 1;
}
}
// Read file
if ( !done ) {
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code == 'ENOENT') {
// Page not found
fs.readFile(path.join(__dirname, 'public', '404.html'), (err, content) => {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end(content, 'utf8');
})
} else {
// Some server error
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
} else {
// Success
res.writeHead(200, { 'Content-Type': type });
res.end(content);
}
})
done = 1;
}
/* function newFunction() {
console.log(path.join(__dirname, 'public', 'images', 'jrm.jpg'));
}*/
})
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Server running on ${PORT}`));
// Using 'npm run dev' which will run nodemon which will run server_demo
// see package.json