-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
86 lines (67 loc) · 1.86 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
85
86
'use strict';
var app = module.exports = require('express')(),
compression = require('compression'),
Cloudant = require('cloudant'),
bodyParser = require('body-parser'),
router = require('./lib/routes/index'),
async = require('async'),
init = require('./lib/init'),
events = require('events'),
ee = new events.EventEmitter(),
morgan = require('morgan'),
cors = require('./lib/cors');
// Required environment variables
var env = require('./lib/env').getCredentials();
var cloudant = new Cloudant(env.couchHost),
dbName = app.dbName = env.databaseName;
app.db = cloudant.db.use(dbName);
app.metaKey = 'com_cloudant_meta';
app.events = ee;
app.cloudant = cloudant;
app.serverURL = env.couchHost;
// Setup the logging format
if (env.logFormat !== 'off') {
app.use(morgan(env.logFormat));
}
function main() {
// enable cors
app.use(cors());
// gzip responses
app.use(compression());
app.use(bodyParser.json({ limit: '50mb'}));
app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' }));
app.use('/', router);
// Catch 404 and forward to error handler.
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handlers
app.use(function(err, req, res) {
console.error(err.stack);
res.status(err.statusCode || 500).send('Something broke!');
});
app.listen(env.port);
}
// Make sure that any init stuff is executed before
// kicking off the app.
async.series(
[
init.verifyDB,
init.verifySecurityDoc,
init.installSystemViews
],
function (err, results) {
for (var result in results) {
console.log(results[result]);
}
if (err != null) {
process.exit(1);
}
main();
ee.emit('listening');
console.log('[OK] main: Started app on', env.url);
}
);
require("cf-deployment-tracker-client").track();