-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.js
110 lines (92 loc) · 3.89 KB
/
index1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// https://www.youtube.com/watch?v=9mnkFo-DJj0
var http = require("http")
var host = '127.0.0.1'
var port = 3000
http.createServer(function (req, res) {
// http.createServer((req, res) => {
console.log('req.....', req.method);
console.log('req.....url', req.url);
if (req.method === "GET") {
if (req.url === "/") {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
// res.setHeader("Content-Type", "text/html")
// res.writeHead(200, { "Content-Type": "text/plain" }) //HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API
// res.end("Hello")
res.write("Hello")
res.end() // to end the process
} else if (req.url === "/about") {
// http.createServer((req, res) => {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
res.end("about") // to end the process
}
}
else if (req.method === "POST") {
if (req.url === "/") {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
// res.setHeader("Content-Type", "text/html")
// res.writeHead(200, { "Content-Type": "text/plain" }) //HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API
// res.end("Hello")
res.write("Hello post")
res.end() // to end the process
} else if (req.url === "/about") {
// http.createServer((req, res) => {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
res.end("about post") // to end the process
} else if (req.url === "/api/blogs" && req.method === "POST") {
try {
let body = "";
// Listen for data event
req.on("data", (chunk) => {
body += chunk.toString();
});
console.log('body', body);
// Listen for end event
req.on("end", async () => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(body));
});
} catch (error) {
console.log(error);
}
}
} else if (req.method === "DELETE") {
if (req.url === "/") {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
// res.setHeader("Content-Type", "text/html")
// res.writeHead(200, { "Content-Type": "text/plain" }) //HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API
// res.end("Hello")
res.write("Hello delete")
res.end() // to end the process
} else if (req.url === "/about") {
// http.createServer((req, res) => {
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
res.end("about delete") // to end the process
}
}
}).listen(port, host, function () {
console.log(`server started at http://${host}:${port}/`);
});
// another page
// const router = async function (req, res) {
// // GET: /api/blogs
// if (req.url === "/api/blogs" && req.method === "GET") {
// // get all blogs
// // set the status code and content-type
// res.writeHead(200, { "Content-Type": "application/json" });
// // send data
// res.end("Get method");
// }
// };
// module.exports = router;
// // const router = require("./routes/blogRoutes");
// const server = http.createServer((req, res) => {
// router(req, res);
// });
// server.listen(3000)
// console.log(`server is listening at ${port}`);