-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
82 lines (66 loc) · 1.94 KB
/
server.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
require('dotenv').config();
const express = require('express');
const logger = require('morgan');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const bodyParser = require('body-parser');
const cors = require('cors');
const errorHandler = require('errorhandler');
const mongoose = require('mongoose');
const UserModel = require("./models/user.model");
let app = express();
let server = require('http').createServer(app);
let mongoUrl = process.env.MONGO_CONNECTION_STRING;
mongoose.set('useUnifiedTopology', true);
mongoose
.connect(mongoUrl, {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => {
console.log('Connected to Local MongoDB');
})
.catch(err => {
console.log('MongoDB connection error. Please make sure MongoDB is running. ' + err);
process.exit();
});
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Passport Config
require('./config/passport')(passport);
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
UserModel.findById(id, function(err, user) {
cb(err, user);
});
});
app.set('port', process.env.SERVER_PORT || 4000);
// allow-cors
app.use(cors());
app.use(logger('tiny'));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(methodOverride());
app.use(cookieParser());
// ******************* call all routes ***************************
app.use('/uploads',express.static('uploads'))
app.use('/api', require('./routes'));
// error handling middleware should be loaded after loading the routes
app.use(errorHandler());
server.listen(app.get('port'), error => {
if (error) {
//console.error(`\n${error}`);
server.close();
process.exit(1);
}
console.log(`Server Listening at http://localhost:${app.get('port')}/`);
});