-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (32 loc) · 989 Bytes
/
server.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
const http = require('http');
const requestListener = (request, response) => {
response.setHeader('Content-Type', 'text/html');
response.statusCode = 200;
const { method } = request;
if(method === 'GET') {
response.end('<h1>Hello!</h1>');
}
if(method === 'POST') {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
});
request.on('end', () => {
body = Buffer.concat(body).toString();
const { name, age } = JSON.parse(body);
response.end(`<h1>Hai, ${name}! umur : ${age}</h1>`);
});
}
if(method === 'PUT') {
response.end('<h1>Bonjour!</h1>');
}
if(method === 'DELETE') {
response.end('<h1>Salam!</h1>');
}
};
const server = http.createServer(requestListener);
const port = 5000;
const host = 'localhost';
server.listen(port, host, () => {
console.log(`Server berjalan pada http://${host}:${port}`);
});