-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
server.js
62 lines (49 loc) · 1.24 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
'use strict';
/*
* nodejs-express-mongoose
* Copyright(c) 2015 Madhusudhan Srinivasa <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies
*/
require('dotenv').config();
const fs = require('fs');
const join = require('path').join;
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const config = require('./config');
const models = join(__dirname, 'app/models');
const port = process.env.PORT || 3000;
const app = express();
const connection = connect();
/**
* Expose
*/
module.exports = {
app,
connection
};
// Bootstrap models
fs.readdirSync(models)
.filter(file => ~file.indexOf('.js'))
.forEach(file => require(join(models, file)));
// Bootstrap routes
require('./config/passport')(passport);
require('./config/express')(app, passport);
require('./config/routes')(app, passport);
connection
.on('error', console.log)
.on('disconnected', connect)
.once('open', listen);
function listen() {
if (app.get('env') === 'test') return;
app.listen(port);
console.log('Express app started on port ' + port);
}
function connect() {
var options = { keepAlive: 1, useNewUrlParser: true };
mongoose.connect(config.db, options);
return mongoose.connection;
}