forked from hafidzdev17/latihan-express-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbelajar_node.js
44 lines (38 loc) · 1.12 KB
/
belajar_node.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
44
const http = require('http');
const hello = require('./helloworld');
const moment = require('moment');
const hostname = '127.0.0.1'; // atau localhost
const port = 3000;
// menampilkan text/html
// const server = http.createServer((req, res) => {
// res.statusCode = 200;
// res.setHeader('Content-Type', 'text/html');
// res.write(moment().calendar());
// res.end();
// });
// menampilkan JSON
// const server = http.createServer((req, res) => {
// res.statusCode = 200;
// res.setHeader('Content-Type', 'text/json');
// res.write(JSON.stringify({
// 'status': 'success',
// 'message': 'response success'
// }));
// res.end();
// });
// Menangani Routing
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const url = req.url
if (url === '/employee') {
res.write('data employee');
} else {
res.write('data apa yang kamu perlukan?');
}
res.end();
});
// menjalankan server
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});