-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (65 loc) · 1.95 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
const express = require('express');
const path = require('path');
const cors = require('cors');
const mongoose = require('mongoose');
// const session = require('express-session');
// const passport = require('passport');
// const expressValidator = require('express-validator');
/* The default server-side session storage, MemoryStore, is purposely not designed for a production environment.
It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing.
For a list of stores, see http://expressjs.com/en/resources/middleware/session.html#compatible-session-stores
*/
// Models & Routes
const app = express();
require('./models/Users')
const errorHandler = require('errorhandler');
require('dotenv').config();
const {
port = 5000,
node_env = 'development',
atlas_uri,
secret,
} = process.env;
const isProduction = node_env === 'production';
mongoose.Promise= global.Promise;
mongoose.connect(atlas_uri, {useNewUrlParser : true, useCreateIndex : true, useUnifiedTopology: true});
mongoose.set('debug',true);
const connection = mongoose.connection
if(!isProduction)
{
app.use(errorHandler());
}
app.use(cors());
app.use(require('morgan')('dev'));
app.use(express.json());
if(!isProduction)
{
app.use((err,req,res,next) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err
}
});
});
}
else
{
app.use((err,req,res,next) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: {}
}
});
});
}
app.use(require('./routes'));
connection.once('open', () => {
console.log('MongoDB Database Connection Established Succesfully');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});