-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (42 loc) · 1.98 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
var http = require('http')
var fs = require('fs')
var url = require('url')
//console.log(Object.keys(http))
var port = process.env.PORT || 8888;
var server = http.createServer(function (request, response) {
var temp = url.parse(request.url, true)
var path = temp.pathname
var query = temp.query
var method = request.method
//从这里开始看,上面不要看
if (path === '/') { // 如果用户请求的是 / 路径
var string = fs.readFileSync('./index.html','utf8') // 就读取 index.html 的内容
var amount = fs.readFileSync('./db','utf8')
string = string.replace('&&&amount&&&',amount)
response.setHeader('Content-Type', 'text/html;charset=utf-8') // 设置响应头 Content-Type
response.end(string) // 设置响应消息体
}else if (path === '/style.css') { // 如果用户请求的是 /style.css 路径
var string = fs.readFileSync('./style.css')
response.setHeader('Content-Type', 'text/css')
response.end(string)
}else if (path === '/main.js') { // 如果用户请求的是 /main.js 路径
var string = fs.readFileSync('./main.js')
response.setHeader('Content-Type', 'application/javascript')
response.end(string)
}else if (path === '/pay'){
var amount = fs.readFileSync('./db','utf8')
var newAmount = amount - 1
fs.writeFileSync('./db',newAmount)
response.end(
`${query.callback}.call(undefined, 'success')`
)
}else { // 如果上面都不是用户请求的路径
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8') // 设置响应头 Content-Type
response.end('找不到对应的路径,你需要自行修改 index.html')
}
// 代码结束,下面不要看
console.log(method + ' ' + request.url)
})
server.listen(port)
console.log('监听 ' + port + ' 成功,请用在空中转体720度然后用电饭煲打开 http://localhost:' + port)