-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
63 lines (53 loc) · 1.29 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
'use strict';
/**
* @file
* @license MPL-2.0
*/
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const deb = require('debug');
const http = require('./http');
const config = require('./config');
deb.enable('lav:server');
const log = deb('lav:server');
const app = express();
const server = http.createServer(app);
const port = process.env.PORT || 3000;
// Enable logging
if (process.env.NODE_ENV === 'development') {
deb.enable('lav:*');
}
if (!process.env.JWT_SECRET) {
// eslint-disable-next-line
console.log('Please configure the server by copying .env.example file to .env');
process.exit(1);
}
// Bootstrap models
require('./app/models/User');
// Bootstrap routes
const data = {app, server};
require('./config/express')(data);
require('./config/routes')(data);
require('./config/socket')(data);
/**
* Connect to mongodb server.
*/
function connect() {
log('connecting to mongodb...');
return mongoose.connect(config.db, config.dbOptions);
}
/**
* Start the server.
*/
function listen() {
if (app.get('env') === 'test') {
return;
}
server.listen(port);
log(`Server started on port ${port}`);
}
connect()
.then(listen)
.catch(log);
module.exports = {app, server};