-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
84 lines (67 loc) · 2.46 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
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
82
83
84
(function () {
"use strict";
require('dotenv').config();
const config = require('./config'),
cors = require('cors'),
express = require('express'),
exphbs = require('express-handlebars'),
fs = require('fs'),
moment = require('moment'),
mongodb = require('./services/mongodb'),
morgan = require('morgan'),
removeExpiredSubscriptions = require('./services/remove-expired-subscriptions');
let app,
expressWs,
hbs,
server;
require('console-stamp')(console, 'HH:MM:ss.l');
console.log(`${config.appName} ${config.appVersion}`);
// TODO: Every 24 hours run removeExpiredSubscriptions(data);
morgan.format('mydate', function() {
var df = require('dateformat');
return df(new Date(), 'HH:MM:ss.l');
});
app = express();
expressWs = require('express-ws')(app);
app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));
app.use(cors());
// Configure handlebars template engine to work with moment
hbs = exphbs.create({
helpers: {
formatDate: function (datetime, format) {
return moment(datetime).format(format);
}
}
});
// Configure express to use handlebars
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// Handle static files in public directory
app.use(express.static('public', {
dotfiles: 'ignore',
maxAge: '1d'
}));
// Load controllers
app.use(require('./controllers'));
// Start server
mongodb.connect('rsscloud', config.mongodbUri)
.then(() => {
server = app.listen(config.port, function () {
app.locals.host = config.domain;
app.locals.port = server.address().port;
if (app.locals.host.indexOf(':') > -1) {
app.locals.host = '[' + app.locals.host + ']';
}
console.log('Listening at http://%s:%s', app.locals.host, app.locals.port);
})
.on('error', function (error) {
switch (error.code) {
case 'EADDRINUSE':
console.log(`Error: Port ${config.port} is already in use.`);
break;
default:
console.log(error.code);
}
});
});
}());