-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (32 loc) · 1.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
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
var tmp = pathname.lastIndexOf(".");
var extension = pathname.substring((tmp + 1));
// Print the name of the file for which request is made.
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
}else {
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
if (extension === 'html') response.writeHeader(200, {"Content-Type": 'text/html'});
else if (extension === 'css') response.writeHeader(200, {"Content-Type": 'text/css'});
else if (extension === 'png') response.writeHeader(200, {"Content-Type": 'image/png'});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');