@ -22,7 +22,8 @@ var express = require('express'),
meta = require ( './meta.js' ) ,
meta = require ( './meta.js' ) ,
feed = require ( './feed' ) ,
feed = require ( './feed' ) ,
plugins = require ( './plugins' ) ,
plugins = require ( './plugins' ) ,
nconf = require ( 'nconf' ) ;
nconf = require ( 'nconf' ) ,
winston = require ( 'winston' ) ;
( function ( app ) {
( function ( app ) {
var templates = null ,
var templates = null ,
@ -72,6 +73,10 @@ var express = require('express'),
} ;
} ;
// Middlewares
// Middlewares
app . configure ( function ( ) {
async . series ( [
function ( next ) {
// Pre-router middlewares
app . use ( express . compress ( ) ) ;
app . use ( express . compress ( ) ) ;
app . use ( express . favicon ( path . join ( _ _dirname , '../' , 'public' , 'favicon.ico' ) ) ) ;
app . use ( express . favicon ( path . join ( _ _dirname , '../' , 'public' , 'favicon.ico' ) ) ) ;
app . use ( require ( 'less-middleware' ) ( {
app . use ( require ( 'less-middleware' ) ( {
@ -81,7 +86,6 @@ var express = require('express'),
} ) ) ;
} ) ) ;
app . use ( nconf . get ( 'relative_path' ) , express . static ( path . join ( _ _dirname , '../' , 'public' ) ) ) ;
app . use ( nconf . get ( 'relative_path' ) , express . static ( path . join ( _ _dirname , '../' , 'public' ) ) ) ;
app . use ( express . bodyParser ( ) ) ; // Puts POST vars in request.body
app . use ( express . bodyParser ( ) ) ; // Puts POST vars in request.body
app . use ( express . cookieParser ( ) ) ; // If you want to parse cookies (res.cookies)
app . use ( express . cookieParser ( ) ) ; // If you want to parse cookies (res.cookies)
app . use ( express . session ( {
app . use ( express . session ( {
store : new RedisStore ( {
store : new RedisStore ( {
@ -95,65 +99,40 @@ var express = require('express'),
}
}
} ) ) ;
} ) ) ;
app . use ( express . csrf ( ) ) ;
app . use ( express . csrf ( ) ) ;
// Local vars, other assorted setup
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
nconf . set ( 'https' , req . secure ) ;
res . locals . csrf _token = req . session . _csrf ;
res . locals . csrf _token = req . session . _csrf ;
next ( ) ;
next ( ) ;
} ) ;
} ) ;
// Static Directories for NodeBB Plugins
// Authentication Routes
app . configure ( function ( ) {
auth . initialize ( app ) ;
var tailMiddlewares = [ ] ;
next ( ) ;
} ,
function ( next ) {
// Static Directories for NodeBB Plugins
plugins . ready ( function ( ) {
plugins . ready ( function ( ) {
// Remove some middlewares until the router is gone
// This is not recommended behaviour: http://stackoverflow.com/a/13691542/122353
// Also: https://www.exratione.com/2013/03/nodejs-abusing-express-3-to-enable-late-addition-of-middleware/
tailMiddlewares . push ( app . stack . pop ( ) ) ;
tailMiddlewares . push ( app . stack . pop ( ) ) ;
tailMiddlewares . push ( app . stack . pop ( ) ) ;
for ( d in plugins . staticDirs ) {
for ( d in plugins . staticDirs ) {
app . use ( nconf . get ( 'relative_path' ) + '/plugins/' + d , express . static ( plugins . staticDirs [ d ] ) ) ;
app . use ( nconf . get ( 'relative_path' ) + '/plugins/' + d , express . static ( plugins . staticDirs [ d ] ) ) ;
winston . info ( 'Static directory routed for plugin: ' + d ) ;
}
}
// Push the removed middlewares back onto the application stack
tailMiddlewares . reverse ( ) ;
app . stack . push ( tailMiddlewares . shift ( ) ) ;
app . stack . push ( tailMiddlewares . shift ( ) ) ;
app . stack . push ( tailMiddlewares . shift ( ) ) ;
} ) ;
} ) ;
module . exports . init = function ( ) {
templates = global . templates ;
// translate all static templates served by webserver here. ex. footer, logout
translator . translate ( templates [ 'footer' ] . toString ( ) , function ( parsedTemplate ) {
templates [ 'footer' ] = parsedTemplate ;
} ) ;
translator . translate ( templates [ 'logout' ] . toString ( ) , function ( parsedTemplate ) {
templates [ 'logout' ] = parsedTemplate ;
} ) ;
server . listen ( nconf . get ( 'PORT' ) || nconf . get ( 'port' ) , nconf . get ( 'bind_address' ) ) ;
}
auth . initialize ( app ) ;
app . use ( function ( req , res , next ) {
nconf . set ( 'https' , req . secure ) ;
next ( ) ;
next ( ) ;
} ) ;
} ) ;
} ,
function ( next ) {
// Router & post-router middlewares
app . use ( app . router ) ;
app . use ( app . router ) ;
// 404 catch-all
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
res . status ( 404 ) ;
res . status ( 404 ) ;
// respond with html page
// respond with html page
if ( req . accepts ( 'html' ) ) {
if ( req . accepts ( 'html' ) ) {
//res.json('404', { url: req.url });
res . redirect ( nconf . get ( 'relative_path' ) + '/404' ) ;
res . redirect ( nconf . get ( 'relative_path' ) + '/404' ) ;
return ;
return ;
}
}
@ -184,12 +163,35 @@ var express = require('express'),
} ) ;
} ) ;
} ) ;
} ) ;
next ( ) ;
}
] , function ( err ) {
if ( err ) {
winston . error ( 'Errors were encountered while attempting to initialise NodeBB.' ) ;
} else {
winston . info ( 'Middlewares loaded.' ) ;
}
} ) ;
} ) ;
module . exports . init = function ( ) {
templates = global . templates ;
// translate all static templates served by webserver here. ex. footer, logout
translator . translate ( templates [ 'footer' ] . toString ( ) , function ( parsedTemplate ) {
templates [ 'footer' ] = parsedTemplate ;
} ) ;
translator . translate ( templates [ 'logout' ] . toString ( ) , function ( parsedTemplate ) {
templates [ 'logout' ] = parsedTemplate ;
} ) ;
server . listen ( nconf . get ( 'PORT' ) || nconf . get ( 'port' ) , nconf . get ( 'bind_address' ) ) ;
}
app . create _route = function ( url , tpl ) { // to remove
app . create _route = function ( url , tpl ) { // to remove
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>' ;
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>' ;
} ;
} ;
app . namespace ( nconf . get ( 'relative_path' ) , function ( ) {
app . namespace ( nconf . get ( 'relative_path' ) , function ( ) {
auth . create _routes ( app ) ;
auth . create _routes ( app ) ;