-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
43 lines (31 loc) · 901 Bytes
/
index.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 http = require('http')
const path = require('path')
const fs = require('fs')
const url = require('url')
function staticRoot(staticPath, req, res){
let pathObj = url.parse(req.url, true)
if(pathObj.pathname==='/') {
pathObj.pathname = '/index.html'
}
console.log(pathObj)
let delay = pathObj.query.t*1000 || 0
let filePath = path.join(staticPath,'public', pathObj.pathname)
fs.readFile(filePath, 'binary', function(err, fileContent){
if(err){
console.log('404')
res.writeHead(404, 'not found')
res.end('<h1>404 Not Found</h1>')
}else{
console.log('ok')
setTimeout(function(){
res.write(fileContent, 'binary')
res.end()
}, delay)
}
})
}
let server = http.createServer(function(req, res){
staticRoot(__dirname, req, res)
})
server.listen(8080)
console.log('打开 http://localhost:8080' )