-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (79 loc) · 2.87 KB
/
app.js
File metadata and controls
84 lines (79 loc) · 2.87 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
// Dynamic Routing:
// const http = require('http');
// const querystring = require('querystring');
// const port = process.env.PORT || 8000;
// const sendPlainText = (req, res) => {
// res.setHeader('Content-Type', 'text/plain');
// res.end('Hi there!');
// };
// const sendJsonData = (req, res) => {
// res.setHeader('Content-Type', 'application/json');
// res.end(JSON.stringify({name : "Limon", roll : 135735, numbers : [1,2,3,4,5]}));
// };
// const respondEcho = (req, res) => {
// const {input = ''} = querystring.parse(
// // '/echo?input=limon'
// // 'input=limon'
// req.url.split('?').slice(1).join('') // http://localhost:8000/echo?input=limon
// );
// res.setHeader('Content-Type', 'application/json');
// res.end(
// JSON.stringify(
// {
// normal : input,
// shouty : input.toUpperCase(),
// lengthCount : input.length,
// backwards : input.split('').reverse().join('')
// }
// )
// );
// }
// const responseNotFound = (req, res) => {
// res.writeHead(404, {'Content-Type' : 'text/html'});
// res.end('<center>404 Not Found<center>');
// }
// const server = http.createServer((req, res) => {
// if (req.url === '/') return sendPlainText(req, res);
// if (req.url === '/json') return sendJsonData(req, res);
// if (req.url.match(/^\/echo/)) return respondEcho(req, res);
// responseNotFound(req, res);
// });
// server.listen(port);
// console.log(`Server is running on port: ${port}.`);
const http = require('http');
const querystring = require('querystring');
const port = process.env.PORT || 3000;
const sendPlainText = (req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.end('Hello there. I am Nahid Hasan Limon');
};
const sendJsonData = (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({name : "Nahid Hasan Limon", age : 20, address : "Barishal, Bangladesh"}));
};
const echoData = (req, res) => {
const {input = ''} = querystring.parse(
req.url.split('?').slice(1).join('')
);
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(
{
normal : input,
upperCase : input.toUpperCase(),
length : input.length,
backwards : input.split('').reverse().join('')
}
))
};
const errorData = (req, res) => {
res.writeHead(404, {'Content-Type' : 'text/html'});
res.end('<center><h1>404 Not Found!</h1></center>');
};
const server = http.createServer((req, res) => {
if (req.url === '/') return sendPlainText(req, res);
if (req.url === '/json') return sendJsonData(req, res);
if (req.url.match(/^\/echo/)) return echoData(req, res);
errorData(req, res);
});
server.listen(port);
console.log('Your server is running on port : ' + port);