-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
server.js
60 lines (48 loc) · 1.22 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
'use strict';
/*
* nodejs-express-mongoose-demo
* Copyright(c) 2013 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();
/**
* Expose
*/
module.exports = app;
// Bootstrap models
fs.readdirSync(models)
.filter(file => ~file.search(/^[^.].*\.js$/))
.forEach(file => require(join(models, file)));
// Bootstrap routes
require('./config/passport')(passport);
require('./config/express')(app, passport);
require('./config/routes')(app, passport);
connect();
function listen() {
if (app.get('env') === 'test') return;
app.listen(port);
console.log('Express app started on port ' + port);
}
function connect() {
mongoose.connection
.on('error', console.log)
.on('disconnected', connect)
.once('open', listen);
return mongoose.connect(config.db, {
keepAlive: 1,
useNewUrlParser: true,
useUnifiedTopology: true
});
}