-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
47 lines (39 loc) · 1.82 KB
/
main.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
/**
* Created by lzc on 2017/6/8.
*/
var http = require('http')
var fs = require('fs')
var url = require('url')
//console.log(Object.keys(http))
var port = process.env.PORT || 9527
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') // 就读取 index.html 的内容
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 === '/print-style.css'){ // 如果用户请求的是 /print-style.css 路径
var string = fs.readFileSync('./print-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{ // 如果上面都不是用户请求的路径
response.setHeader('Content-Type', 'text/html;charset=utf-8') // 设置响应头 Content-Type
response.end('找不到对应的路径,你需要自行修改 index.js')
}
// 代码结束,下面不要看
console.log(method + ' ' + request.url)
})
server.listen(port)
console.log('监听 ' + port + ' 成功,请用浏览器打开 http://localhost:' + port)