-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
95 lines (87 loc) · 2.17 KB
/
app.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
//Importing all packages
const express = require('express');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const morgan = require('morgan');
const app = express();
dotenv.config({ path: '.env' });
// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
// Importing all the modals form modals.js
const {
user,
location,
official,
token,
token_official,
} = require('./Database/modals.js');
const conn = async () => {
try {
//connecting to db
const db = await mongoose.connect(process.env.DB_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
console.log('connected to db');
} catch (error) {
console.log(error);
}
};
//Calling Function to connect to mongodb
conn();
//Adding the user object to every Incoming request
app.use((req, res, next) => {
req.body.object_user = {
Error: "null",
Email: "null",
Name: "null",
Token: "null",
Requests: "null",
Message: "null",
Status: false,
Pending_request:[],
Solved_request:[],
Phn_no:"null",
Address:"null",
};
next();
});
// Adding the official object to every Incoming request
app.use((req, res, next) => {
req.body.official_object = {
error: null,
msg: null,
employee_id: null,
email:null,
name: null,
token: null,
issues_solved: null,
issues_pending: null,
status: null,
};
next();
});
//Resolving CORS errors
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Content-Type', 'application/json');
next();
});
//routes
app.get('/hello', (req, res) => {
res.status(200).send(JSON.stringify({ msg: 'hello' }));
});
app.use('/user', require('./Routes/user_auth.js'));
app.use('/official', require('./Routes/official_auth.js'));
app.use('/check-official', require('./Routes/official_status.js'));
app.use('/check-user', require('./Routes/user_status.js'));
//starting server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log('server started');
});