UNPKG

4.77 kBJavaScriptView Raw
1var express = require('express');
2var path = require('path');
3var favicon = require('serve-favicon');
4var logger = require('morgan');
5var cookieParser = require('cookie-parser');
6var bodyParser = require('body-parser');
7var MongoClient = require('mongodb').MongoClient;
8var handlebars = require('express-handlebars');
9var nconf = require('nconf');
10
11var routes = require('./routes/index');
12
13var app = express();
14
15// view engine setup
16app.set('views', path.join(__dirname, 'views'));
17app.engine('hbs', handlebars({ extname: 'hbs', defaultLayout: 'layout.hbs' }));
18app.set('view engine', 'hbs');
19
20// helpers for the handlebars templating platform
21handlebars = handlebars.create({
22 helpers: {
23 toJSON : function(object) {
24 return JSON.stringify(object);
25 },
26 niceBool : function(object) {
27 if(object === undefined){
28 return "No";
29 }
30 if(object === true){
31 return "Yes";
32 }else{
33 return "No";
34 }
35 },
36 formatBytes : function(bytes) {
37 if(bytes == 0) return '0 Byte';
38 var k = 1000;
39 var decimals = 2;
40 var dm = decimals + 1 || 3;
41 var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
42 var i = Math.floor(Math.log(bytes) / Math.log(k));
43 return (bytes / Math.pow(k, i)).toPrecision(dm) + ' ' + sizes[i];
44 }
45 }
46});
47
48app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
49app.use(logger('dev'));
50app.use(bodyParser.json({limit: '16mb'}));
51app.use(bodyParser.urlencoded({extended: false }));
52app.use(cookieParser());
53
54// front-end modules loaded from NPM
55app.use("/ace", express.static(path.join(__dirname, 'node_modules/ace-builds/src-min/')));
56app.use("/font-awesome", express.static(path.join(__dirname, 'node_modules/font-awesome/')));
57app.use("/jquery", express.static(path.join(__dirname, 'node_modules/jquery/dist/')));
58app.use("/bootstrap", express.static(path.join(__dirname, 'node_modules/bootstrap/dist/')));
59app.use(express.static(path.join(__dirname, 'public')));
60
61// setup nconf to read in the file
62
63// create config dir and blank files if they dont exist
64var fs = require('fs');
65if (!fs.existsSync("config")){
66 fs.mkdirSync("config");
67}
68if (!fs.existsSync("config/config.json")){
69 fs.writeFileSync("config/config.json", "{}");
70}
71if (!fs.existsSync("config/app.json")){
72 fs.writeFileSync("config/app.json", "{}");
73}
74
75var connection_config = path.join(__dirname, 'config', 'config.json');
76var app_config = path.join(__dirname, 'config', 'app.json');
77
78// if config files exist but are blank we write blank files for nconf
79if (fs.existsSync(app_config, "utf8")) {
80 if(fs.readFileSync(app_config, "utf8") == ""){
81 fs.writeFileSync(app_config, "{}", 'utf8');
82 }
83}
84if (fs.existsSync(connection_config, "utf8")) {
85 if(fs.readFileSync(connection_config, "utf8") == ""){
86 fs.writeFileSync(connection_config, "{}", 'utf8');
87 }
88}
89
90// setup the two conf. 'app' holds application config, and connections
91// holds the mongoDB connections
92nconf.add('connections', { type: 'file', file: connection_config });
93nconf.add('app', { type: 'file', file: app_config });
94
95// set app defaults
96var app_host = '0.0.0.0';
97var app_port = 1234;
98
99// get the app configs and override if present
100if(nconf.stores.app.get('app:host') != undefined){
101 app_host = nconf.stores.app.get('app:host');
102}
103if(nconf.stores.app.get('app:port') != undefined){
104 app_port = nconf.stores.app.get('app:port');
105}
106
107// Make stuff accessible to our router
108app.use(function (req, res, next) {
109 req.nconf = nconf.stores.connections;
110 req.handlebars = handlebars;
111 next();
112});
113
114app.use('/', routes);
115
116// catch 404 and forward to error handler
117app.use(function (req, res, next) {
118 var err = new Error('Not Found');
119 err.status = 404;
120 next(err);
121});
122
123// error handlers
124
125// development error handler
126// will print stacktrace
127if (app.get('env') === 'development') {
128 app.use(function (err, req, res, next) {
129 res.status(err.status || 500);
130 res.render('error', {
131 message: err.message,
132 error: err
133 });
134 });
135}
136
137// production error handler
138// no stacktraces leaked to user
139app.use(function (err, req, res, next) {
140 res.status(err.status || 500);
141 res.render('error', {
142 message: err.message,
143 error: {}
144 });
145});
146
147
148// lift the app
149app.listen(app_port, app_host, function () {
150 console.log('adminMongo listening on host: http://' + app_host + ':' + app_port);
151});
152
153module.exports = app;