-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
74 lines (60 loc) · 2.33 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
69
70
71
72
73
74
var http = require('http'),
fs = require('fs');
// http.createServer(function(request, response) {
// response.writeHead(200, {"Content-Type": "text/plain"});
// response.write("Hello World");
// response.end();
// }).listen(process.env.PORT || 7777);
// fs.readFile('index.html', function (err, html) {
// if (err) {
// throw err;
// }
// http.createServer(function(request, response) {
// response.writeHeader(200, {"Content-Type": "text/html"});
// response.write(html);
// response.end();
// }).listen(process.env.PORT || 7777);
// });
http.createServer(function (req, res) {
console.log(req.url);
if (req.url == '/' || req.url.indexOf('.html') != -1) { //req.url has the pathname, check if it conatins '.html'
fs.readFile(__dirname + '/index.html', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
} else {
fs.readFile(__dirname + req.url, function (err, data) {
if (err) console.log(err);
res.writeHead(200);
res.write(data);
res.end();
});
}
// if (req.url == '/' || req.url.indexOf('.html') != -1) { //req.url has the pathname, check if it conatins '.html'
// fs.readFile(__dirname + '/index.html', function (err, data) {
// if (err) console.log(err);
// res.writeHead(200, {'Content-Type': 'text/html'});
// res.write(data);
// res.end();
// });
// }
// if(req.url.indexOf('.js') != -1) { //req.url has the pathname, check if it conatins '.js'
// fs.readFile(__dirname + '/js/app.min.js', function (err, data) {
// if (err) console.log(err);
// res.writeHead(200, {'Content-Type': 'text/javascript'});
// res.write(data);
// res.end();
// });
// }
// if(req.url.indexOf('.css') != -1) { //req.url has the pathname, check if it conatins '.css'
// fs.readFile(__dirname + '/css/index.css', function (err, data) {
// if (err) console.log(err);
// res.writeHead(200, {'Content-Type': 'text/css'});
// res.write(data);
// res.end();
// });
// }
}).listen(process.env.PORT || 7777);
console.log('Server running at http://127.0.0.1:', process.env.PORT || 7777, '/');