forked from jenglish/ssptool
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (64 loc) · 2.01 KB
/
app.js
File metadata and controls
77 lines (64 loc) · 2.01 KB
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
var express = require('express')
, path = require('path')
, favicon = require('serve-favicon')
, logger = require('morgan')
, package = require('./package.json')
, routes = require('./lib/routes')
, mdparser = require('./lib/opencontrol/mdparser')
, app = express()
;
function basepath (p) { return path.join(__dirname, p); }
/** Add logging middleware based on NODE_ENV
*/
function chooseLogger (app) {
let env = app.get('env');
if (env === 'development') {
app.use(logger('dev'));
} else if (env !== 'test') {
app.use(logger('combined'));
}
}
function notFoundHandler (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
}
function errorHandler (err, req, res, next) {
if (res.headersSent) { return next(err); }
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.path = req.path;
res.status(err.status || 500);
res.render('error');
}
app.set('views', basepath('views'));
app.set('view engine', 'pug');
app.locals.appname = package.name;
app.locals.appversion = package.version;
app.locals.apphomepage = package.homepage;
app.use(favicon(basepath('public/favicon.ico')));
chooseLogger(app);
app.use(express.static(basepath('public'), { maxAge: 1000 * 60 * 60 }));
app.use(routes.router);
app.use(notFoundHandler);
app.use(errorHandler);
/**
* Initialization.
*
* @param (Config) config
* @param (opencontrol.Database) db
*/
app.initialize = function (config, db) {
app.set('config', config);
app.set('db', db);
app.set('sitemap', routes.sitemap);
app.locals.appurl = routes.appurl;
app.locals.linkto = routes.linkto;
app.locals.markdown = mdparser.parse;
routes.buildSite(config, db);
routes.router.use('/pages', express.static(config.docdir));
routes.router.use('/assets', express.static(config.assetsdir||'./assets'));
app.locals.toplinks = routes.sitemap.toplinks;
app.locals.config = config;
};
module.exports = app;