-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
119 lines (101 loc) · 3.69 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @package - MidClan - WebApi
* @description - This is the Web API for the MidClan project
*/
global.__basedir = __dirname;
// const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const bodyParser = require("body-parser");
const customEnv = require("custom-env").env();
const cors = require("cors");
const corsConfig = require("./config/cors");
const Respond = require('./services/responses');
const socketEvents = require('./services/socketEvents');
const indexRoute = require('./routes/index');
// var usersRouter = require('./routes/users');
const app = express();
// Routes
const loginRoute = require('./routes/authRoutes/login');
const signupRoute = require('./routes/authRoutes/signup');
const usersRoute = require('./routes/userRoutes/index');
const pharmacistsRoute = require('./routes/pharmacistRoutes/index');
const techniciansRoute = require('./routes/technicianRoutes/index');
const doctorsRoute = require('./routes/doctorRoutes/index');
const postsRoute = require('./routes/postRoutes/index');
const bookmarkRoute = require('./routes/bookmarkRoutes/index');
const chatRoute = require('./routes/chatRoutes/index');
const subscriptionRoute = require('./routes/subscriptionRoutes/index');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(logger('dev'));
// app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(
bodyParser.urlencoded({
limit: '50mb',
extended: true
})
);
// app.use(express.json());
require('./config/mongo'); //connect to mongo database
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
/* *
* Cors is enabled so the client can acces enpoint on this API wthout having to make request *
* from the same Origin
*/
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", corsConfig.origins);
res.header("Access-Control-Allow-Headers", corsConfig.headers);
if (req.method === "OPTIONS") {
//preflight request
res.header("Access-Control-Allow-Methods", corsConfig.methods);
return res.status(200).json({});
}
next();
});
// Routes are enabled below
// Authentication Routes
app.use('/', indexRoute);
app.use('/api/v1/auth/', loginRoute);
app.use('/api/v1/auth/', signupRoute);
app.use('/api/v1/users/', usersRoute);
app.use('/api/v1/pharmacists/', pharmacistsRoute);
app.use('/api/v1/technicians/', techniciansRoute);
app.use('/api/v1/doctors/', doctorsRoute);
app.use('/api/v1/posts/', postsRoute);
app.use('/api/v1/bookmark/', bookmarkRoute);
app.use('/api/v1/chat', chatRoute);
app.use('/api/v1/subscription', subscriptionRoute);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
// next(createError(404));
next(Respond(res).error(404, 'RouteNotFound', 'This is the end of the earth'));
});
// refactored to work on both dev and prod.
let port;
if (app.get('env') === 'development') {
port = 3000 || process.env.PORT;
} else {
port = process.env.PORT;
}
const server = app.listen(process.env.TEST_PORT);
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})
const io = require('socket.io').listen(server);
socketEvents(io);
module.exports = app;