-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
30 lines (20 loc) · 813 Bytes
/
index.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
const responseController = require("./controller")
const http = require("http")
const port = process.env.PORT || 3000
const requestListener = function (request, response) {
if (request.method !== 'POST') {
response.writeHead(400,{'Content-Type': 'application/json'});
response.end(`{"error": "${http.STATUS_CODES[405]}"}`)
} else {
request.on("data",async (chunk)=>{
const input = await JSON.parse(chunk.toString())
const code = input.code
responseController(code, response)
})
request.on("error",()=>response.end(`{"error": "${http.STATUS_CODES[404]}"}`))
}
}
const server = http.createServer(requestListener);
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
})