This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (108 loc) · 3.66 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// importing app components
const express = require('express');
const app = express();
const mongoose = require('mongoose');
require('dotenv').config();
// this will have the environment and configuration for the application
const appConfig = require('./config');
const dbConnString = require('./config').dbConn;
// import routes
const authRoute = require('./api/v1/routes/auth');
const apiUsageRoute = require('./api/v1/routes/api_usage');
const todoRoute = require('./api/v1/routes/todo');
_setupExpressAndAPIv1();
_connectToDB();
function _setupExpressAndAPIv1() {
// body parser
app.use(express.json());
app.use(express.static(__dirname + '/docs'));
// logging requests to console
app.use((req, res, next) => {
console.log(`Request: ${req.method} ${req.url} - ${new Date().toString()}`);
next();
});
// setting routes----------------------------------
// user auth route
app.use('/api/v1/auth', authRoute);
// api usage route
app.use('/api/v1/usage', apiUsageRoute);
// todo route
app.use('/api/v1/todo', todoRoute);
// handling 404 routes
app.use(function(req, res, next) {
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.send('<h2> 404 Not found</h2>');
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
}
// connecting to db
function _connectToDB() {
console.log('Connecting to db...');
mongoose.connect(
dbConnString(appConfig), {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true
},
(error) => {
if (error) {
console.log('Unable to connect to db');
console.log(error);
} else {
console.log(`Connected to db successfully`);
_startServer();
}
});
}
// listen to the port
function _startServer() {
console.log('Starting server...');
app.listen(appConfig.app.port, (err) => {
if (err) {
console.log(`Failed to listen on port ${appConfig.app.port}`)
console.log(err);
} else {
console.log(`Listening on port ${appConfig.app.port}`);
_printRunningEnvironment();
}
})
}
// prints the environment the app is running currently along with the Local network IP
function _printRunningEnvironment() {
// importing modules
const os = require('os');
const ifaces = os.networkInterfaces();
let _lanIP;
Object.keys(ifaces).forEach(function(ifname) {
var alias = 0;
ifaces[ifname].forEach(function(iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
// console.log(ifname + ':' + alias, iface.address);
_lanIP = iface.address;
} else {
// this interface has only one ipv4 adress
// console.log(ifname, iface.address);
_lanIP = iface.address;
}
++alias;
});
});
console.log(`App running on ${appConfig.name} environment`);
console.log(`Run app on local machine http://127.0.0.1:${appConfig.app.port}`);
console.log(`Run app over local network http://${_lanIP}:${appConfig.app.port}`);
}