-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (62 loc) · 1.92 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
import dbConnection from './db/dbconnect.js';
import dotenv from 'dotenv';
import morgan from 'morgan';
// Appjs file ko index file me refrence dege
import App from './app.js'
// As early as possible in your application , import and configure dotenv
// So in first running file we need to execute dotenv.config();
// Which means the env variable is avaible throughout the code
// dotenv.config();
dotenv.config({
path: './env'
});
// This is App Js File
const app = App;
/*
// In index file , make database connection code ,
// this is not good approach , shift that dbconnection code into db file , for proper structure .
// This is async IFI(Imeditaely Invoked Function)
(async () => {
try {
const conn = await mongoose.connect(`${process.env.DATABASE_URI}/${DB_NAME}`);
console.log(`Connected To MongoDB at ${conn.connection.host}`);
// If not able to connect , throw error
// Listners
app.on("error", (error) => {
console.log(`Error in MongoDB is ${error}`);
throw error;
});
app.listen(process.env.PORT, () => {
console.log(`App is Running on port ${process.env.PORT}`);
});
} catch (error) {
console.log(`Error in MongoDB is ${error}`);
throw error;
}
})();
*/
const port = process.env.PORT || 3000
dbConnection()
.then(
// IFI -> This is async IFI(Imeditaely Invoked Function)
() => {
app.listen(port, () => {
console.log(`Server is Running at : ${port}`);
})
}).catch((error) => {
console.log(`Error in MongoDB is ${error}`);
throw error;
})
app.use(morgan('dev'))
app.get('/', (req, res) => {
res.json({
ok: true,
message: "This is demo get Request "
})
})
app.get('/index', (req, res) => {
res.json({
ok: true,
message: "This is Index File "
})
})