forked from chefsecure/servers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
32 lines (27 loc) · 860 Bytes
/
http.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
const http = require('http')
const port = 8000
http.createServer((req, res) => {
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
'Access-Control-Max-Age': 2592000, // 30 days
'Access-Control-Allow-Headers': 'Content-Type',
}
if (req.method === 'OPTIONS') {
res.writeHead(204, headers)
res.end()
return
}
if (['GET', 'POST'].indexOf(req.method) > -1) {
console.log(decodeURIComponent(req.url))
let body = []
req.on('data', data => body.push(data))
.on('end', () => console.log(Buffer.concat(body).toString()))
res.writeHead(200, headers)
res.end('Hello World')
return
}
res.writeHead(405, headers)
res.end(`${req.method} is not allowed for the request.`)
}).listen(port)
console.log(`listening on localhost port ${port}`)