-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
72 lines (46 loc) · 1.39 KB
/
app.ts
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
//----Natif modules import
import express from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import Logger from './loggers/Logger';
import dotenv from 'dotenv';
import router from './routers/index.router';
dotenv.config( {path : './.env'});
//-----Routers import
//----Env Variables
let NODE_ENV:string|undefined = process.env.NODE_ENV;
let FRONTEND:string|undefined = process.env.FRONTEND;
let DB_ENV:string|undefined = process.env.DB_ENV;
//----Created application express
const app:express.Application = express();
//----Cors config
const corsOptions ={
origin:`*`,
credentials:true,//access-control-allow-credentials:true
optionSuccessStatus:200
}
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//----database connection
const connectToDb = async () => {
if(DB_ENV) {
mongoose.set('strictQuery', false);
mongoose.connect(DB_ENV).then( () => {
Logger.info('Connecting to mongoDB Successfully ...');
})
.catch( (error) => {
Logger.error('Connecting to mongoDB ...', error);
process.exit(1);
});
} else {
throw new Error("Env variable it not define");
}
}
if(NODE_ENV){
connectToDb();
}else{
throw new Error("NODE_ENV is missing");
}
app.use(router);
export default app;