-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
54 lines (47 loc) · 1.29 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
let http = require('http');
let fs = require('fs');
let path = require('path');
const port=8000;
let url=require('url')
path.join(__dirname)
const host = 'localhost';
let server=http.createServer(function (request, response) {
let filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
let extname = path.extname(filePath);
let contentType ="";
switch (extname) {
case '.js':
contentType="text/javascript"
break;
case '.css':
contentType=
"text/css"
break;
case '.json':
contentType=
"application/json"
break;
case '.png':
contentType="image/png"
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(response.writeHead(404)){
response.writeHead(404);
response.end('error 404 page not found!');
}
else {
response.writeHead(500);
response.end('error 500');
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
});
server.listen(8000);