-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-2.js
More file actions
32 lines (28 loc) · 752 Bytes
/
5-2.js
File metadata and controls
32 lines (28 loc) · 752 Bytes
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
const http = require('http')
const fs = require('fs')
const qs = require('querystring')
const Url = require('url')
const server = http.createServer((req, res) => {
const obj = Url.parse(req.url, true)
console.log(obj.pathname)
console.log(obj.query) //obtain 'get' data
let str = ''
req.on('data', (data) => {
str += data //obtain 'post' data
})
req.on('end', () => {
const json = qs.parse(str)
console.log(json)
})
const url = './static' + obj.pathname
fs.readFile(url, (err, data) => {
if(err) {
res.write('not found')
}
else {
res.write(data)
}
res.end()
})
})
server.listen(8088)