Skip to content

Commit c6fd058

Browse files
committed
creating http server
1 parent a5d1937 commit c6fd058

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

HTTP-SERVER/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const http = require("http");
2+
3+
// specific port
4+
const PORT = 3000;
5+
6+
const server = http.createServer((req, res) => {
7+
/*
8+
// write some headers to response
9+
res.writeHead(200, {
10+
"Content-Type": "text/plain",
11+
});
12+
13+
// ready to complete the response (close response)
14+
res.end("Hello! Sir Isaac Newton is your friend!");
15+
16+
*/
17+
18+
// json response
19+
res.writeHead(200, {
20+
"Content-Type": "application/json",
21+
});
22+
23+
// JSON.stringify() to convert object into string
24+
res.end(
25+
JSON.stringify({
26+
id: 1,
27+
name: "Sir Issac Newton",
28+
})
29+
);
30+
});
31+
32+
// listen to request
33+
server.listen(PORT, () => {
34+
console.log(`Listening on port ${PORT}...`);
35+
});

0 commit comments

Comments
 (0)