This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
forked from enketo/enketo-express
-
Notifications
You must be signed in to change notification settings - Fork 90
/
express.js
124 lines (113 loc) · 4.32 KB
/
express.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
var express = require( 'express' );
var path = require( 'path' );
var bodyParser = require( 'body-parser' );
var cookieParser = require( 'cookie-parser' );
var fs = require( 'fs' );
var favicon = require( 'serve-favicon' );
var config = require( '../app/models/config-model' ).server;
var logger = require( 'morgan' );
var i18next = require( 'i18next' );
var I18nextBackend = require( 'i18next-node-fs-backend' );
var i18nextMiddleware = require( 'i18next-express-middleware' );
var compression = require( 'compression' );
var errorHandler = require( '../app/controllers/error-handler' );
var controllersPath = path.join( __dirname, '../app/controllers' );
var app = express();
var debug = require( 'debug' )( 'express' );
// general
for ( var item in config ) {
if ( Object.prototype.hasOwnProperty.call( config, item ) ) {
app.set( item, app.get( item ) || config[ item ] );
}
}
app.set( 'port', process.env.PORT || app.get( 'port' ) || 3000 );
app.set( 'env', process.env.NODE_ENV || 'production' );
app.set( 'authentication cookie name', '__enketo_' );
// views
app.set( 'views', path.resolve( __dirname, '../app/views' ) );
app.set( 'view engine', 'pug' );
// pretty json API responses
app.set( 'json spaces', 4 );
// setup i18next
i18next
.use( i18nextMiddleware.LanguageDetector )
.use( I18nextBackend )
.init( {
//debug: true, // DEBUG
whitelist: app.get( 'languages supported' ),
fallbackLng: 'en',
joinArrays: '\n',
backend: {
loadPath: path.resolve( __dirname, '../locales/build/__lng__/translation-combined.json' )
},
load: 'languageOnly',
lowerCaseLng: true,
detection: {
order: [ 'querystring', 'header' ],
lookupQuerystring: 'lang',
caches: false,
},
interpolation: {
prefix: '__',
suffix: '__'
}
} );
// middleware
app.use( compression() );
app.use( bodyParser.json( {
limit: config[ 'payload limit' ]
} ) );
app.use( bodyParser.urlencoded( {
limit: config[ 'payload limit' ],
extended: true
} ) );
app.use( cookieParser( app.get( 'encryption key' ) ) );
app.use( i18nextMiddleware.handle( i18next, {
/*ignoreRoutes: [ '/css', '/fonts', '/images', '/js' ]*/
} ) );
app.use( favicon( path.resolve( __dirname, '../public/images/favicon.ico' ) ) );
app.use( app.get( 'base path' ), express.static( path.resolve( __dirname, '../public' ) ) );
app.use( `${app.get( 'base path' )}/x`, express.static( path.resolve( __dirname, '../public' ) ) );
app.use( app.get( 'base path' ) + '/locales/build', express.static( path.resolve( __dirname, '../locales/build' ) ) );
app.use( `${app.get( 'base path' )}/x` + '/locales/build', express.static( path.resolve( __dirname, '../locales/build' ) ) );
// set variables that should be accessible in all view templates
app.use( function( req, res, next ) {
res.locals.livereload = req.app.get( 'env' ) === 'development';
res.locals.environment = req.app.get( 'env' );
res.locals.analytics = req.app.get( 'analytics' );
res.locals.googleAnalytics = {
ua: req.app.get( 'google' ).analytics.ua,
domain: req.app.get( 'google' ).analytics.domain || 'auto'
};
res.locals.piwikAnalytics = {
trackerUrl: req.app.get( 'piwik' ).analytics[ 'tracker url' ],
siteId: req.app.get( 'piwik' ).analytics[ 'site id' ]
};
res.locals.logo = req.app.get( 'logo' );
res.locals.defaultTheme = req.app.get( 'default theme' ).replace( 'theme-', '' ) || 'kobo';
res.locals.title = req.app.get( 'app name' );
res.locals.dir = function( lng ) {
return i18next.dir( lng );
};
res.locals.basePath = req.app.get( 'base path' );
res.locals.draftEnabled = !req.app.get( 'disable save as draft' );
next();
} );
// load controllers (including their routers)
fs.readdirSync( controllersPath ).forEach( function( file ) {
if ( file.indexOf( '-controller.js' ) >= 0 ) {
debug( 'loading', file );
require( controllersPath + '/' + file )( app );
}
} );
// logging
app.use( logger( ( app.get( 'env' ) === 'development' ? 'dev' : 'tiny' ) ) );
// error handlers
app.use( errorHandler[ '404' ] );
if ( app.get( 'env' ) === 'development' ) {
app.use( errorHandler.development );
} else {
app.use( errorHandler.production );
}
module.exports = app;