-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
52 lines (44 loc) · 1.4 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
/**
* Main application file
*/
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const express = require('express');
const cors = require('cors');
const path = require('path');
const mongoose = require('mongoose');
const config = require('./config/environment');
// Swagger
const swaggerTools = require('swagger-tools');
const YAML = require('yamljs');
const swaggerDoc = YAML.load(path.join(__dirname, 'swagger.yaml'));
// Connect to database
mongoose.Promise = Promise;
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
}
);
// Populate DB with sample data
if(config.seedDB) { require('./config/seed'); }
// Setup server
const app = express();
app.use(cors());
const server = require('http').createServer(app);
const socketio = require('socket.io')(server);
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
// Swagger
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
});
// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
// Expose app
module.exports = app;