-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeServer.js
41 lines (39 loc) · 1.01 KB
/
snakeServer.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
const PORT=8000;
const http = require('http');
const fs = require('fs');
const getContentType=function(file){
let contentTypes = {
'js':'text/javascript',
'html':'text/html',
'css':'text/css',
'jpeg':'image/jpeg',
'txt':'text/plain',
'pdf':'text/plain',
'jpg':'image/jpg',
'gif':'image/gif',
}
let fileExtension = file.split(".").slice(-1)[0];
return contentTypes[fileExtension];
}
const requestHandler = function(req,res){
let file = req.url=='/'? 'index.html' :req.url.slice(1);
if(fs.existsSync(file)){
if(req.method=="POST"){
req.on('data',function(text){
storeComment(text.toString());
});
return;
}
let contentType = getContentType(file);
res.setHeader('content-Type',contentType);
res.statusCode = 200;
res.write(fs.readFileSync(file));
}else{
res.statusCode = 404;
res.write('404 file not found')
}
res.end();
}
const server = http.createServer(requestHandler);
server.listen(PORT);
console.log(`listning to ....${PORT}`);