-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
37 lines (30 loc) · 798 Bytes
/
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
const http = require('http');
const fs = require('fs');
http.createServer(function(request, response) {
const url = request.url;
if (url === "/") {
fs.readFile("index.html", function(err, data) {
if (err) {
response.writeHead(404);
response.write("Not Found!")
} else {
response.writeHead(200, {'Content-Type' : "text/html"});
response.write(data);
}
response.end();
});
} else if (url.startsWith("/example")) {
const fileToRead = url.slice(1, url.length);
fs.readFile(fileToRead, function(err, data) {
if (err) {
response.writeHead(404);
response.write("Not Found!")
} else {
response.writeHead(200, {'Content-Type' : "text/html"});
response.write(data);
}
response.end();
});
}
}).listen(3000);
console.log("Listening on port 3000...");