-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
108 lines (80 loc) · 2.18 KB
/
practice.js
File metadata and controls
108 lines (80 loc) · 2.18 KB
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
//USING THE AXIOS PACKAGE TO CREATE HTTPS REQUESTS
// const axios = require("axios");
// axios
// .get('https://example.com/todos')
// .then(res => {
// console.log(`statusCode: ${res.status}`);
// console.log(res)
// })
// .catch(error => {
// console.error(error)
// })
//TRADITIONAL NODE METHOD TO BUILD HTTP REQUESTS USING HTTP MODULE
// const https = require("https");
// const options = {
// hostname: 'api.facebook.com',
// port:443,
// path: '/todos',
// method: 'GET',
// };
// const req = https.request(options, res => {
// console.log(`statusCode: ${res.statusCode}`)
// res.on("data", d => {
// process.stdout.write(d);
// });
// });
// req.on("error", error => {
// console.error(error);
// })
// req.end();
//AXIOS METHOD TO SEND POST METHODS
// const axios = require('axios');
// axios
// .post("https://whatever.com/todos", {
// todo: "Buy the milk",
// })
// .then(res => {
// console.log(`statusCode: ${res.status}`);
// console.log(res);
// })
// .catch(error => {
// console.error(error);
// })
//ORIGINAL NODE METHOD TO SEND POST METHODS
// const https = require('https');
// const data = JSON.stringify({
// todo: 'Buy the milk',
// });
// const options = {
// hostname: 'whatever.com',
// port: 443,
// path:'/todos',
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'Content-Length': data.length,
// },
// };
// const req = https.request(options, res => {
// console.log(`statusCode: ${res.statusCode}`);
// res.on('data', d => {
// process.stdout.write(d);
// });
// });
// req.on('error', error => {
// console.error(error);
// });
// req.write(data);
// req.end();
//THE HTTP MODULE
//CREATING A SIMPLE HTTP MODULE
const http = require("http")
const server = http.createServer((req,res)=> {
console.log(`${req.method} ${req.url}`);
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello from Node server")
});
server.listen(3000, () => {
console.log("Server listening on http://localhost:3000")
})