-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (48 loc) · 1.34 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
const http = require("http")
const fs = require("fs")
const url = require ("url")
const path = require("path")
const mimeTypes = {
"html" : "text/html",
"jpg" : "image/jpeg",
"jpeg" : "image/jpg",
"png" : "image/png",
"webp" : "image/webp",
"js" : "text/javascript",
"json" : "application/json",
"css" : "text/css"
}
http.createServer((request,response) => {
let pathname = url.parse(request.url).pathname
let filename = "";
if(pathname ==='/'){
filename = "./index.html";
}else filename = path.join(process.cwd(),pathname)
try{
fs.accessSync(filename, fs.F_OK)
let fileStream = fs.createReadStream(filename)
mimeType = mimeTypes[path.extname(filename).split('.')[1]]
response.writeHeader(200, {"content-type": mimeType})
fileStream.pipe(response)
}catch(e){
console.log("file does not exist: " + filename)
response.writeHeader(404, {"content-type": "text/plain"})
response.write("Not Found\n")
response.end()
return
}
return
}).listen(process.env.PORT || 5000)
/*
const http = require("http")
const fs = require("fs")
const PORT = 8080;
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(PORT)
})
*/