-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.46 KB
/
index.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
/*
* Title: Primary File for the CLI App
* Description: Index or Primary file for cli app
* Author: Mohammad Mesbaul Haque
* Github link: https://github.com/mmesba
* Date: 20/01/2022
*/
// Dependencies.
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const url = require('url');
const { StringDecoder } = require('string_decoder');
const cli = require('./cli');
// App object or Module scaffolding.
const server = {};
// main functions or objects.
// Creating http server
server.createHttpServer = ()=>{
const createHttpServerVariable = http.createServer(server.handleReqRes);
createHttpServerVariable.listen(3000, ()=>{
console.log('\x1b[33m%s\x1b', 'Listening CLI App');
})
}
// Defining handle req res function
server.handleReqRes = (req, res)=>{
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const trimmedPath = path.replace(/^\/+\/+$/g, '');
const method = req.method.toLowerCase();
const queryStringObject = parsedUrl.query;
const headersObject = req.headers;
res.setHeader('Content-Type', 'Application/json')
// res.writeHead(statusCode)
res.write('ok')
console.log('\x1b[33m%s\x1b', 'pinging...');
res.end();
}
server.createHttpServer();
// Start the cli functions with delay so it appear later in terminal
setTimeout(() => {
cli.init()
}, 200);
// export the module.
module.exports = server;