-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (41 loc) · 1.21 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
const PORT = 8080
const http = require('http')
const url=require('url')
const fs=require('fs')
const mine=require('./mime').types
const path=require('path')
const server = http.createServer(function (request, response) {
const pathname = url.parse(request.url).pathname
console.log(pathname)
const realPath = path.join("assets", pathname)
console.log(realPath)
let ext = path.extname(realPath)
ext = ext ? ext.slice(1) : 'unknown'
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
})
response.write("This request URL " + pathname + " was not found on this server.")
response.end()
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
})
response.end(err)
} else {
const contentType = mine[ext] || "text/plain"
response.writeHead(200, {
'Content-Type': contentType
})
response.write(file, "binary")
response.end()
}
})
}
})
})
server.listen(PORT)
console.log("Server runing at port: " + PORT + ".")