-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.js
34 lines (29 loc) · 924 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
33
34
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) {
// decode and console.log
console.log(decodeURIComponent(req.url))
let body = ''
// in case of post console.log the object send in the body
req.on('data', data => body += data)
.on('end', () => console.log(body))
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}`)