Skip to content

Instantly share code, notes, and snippets.

@psychobunny
Created March 1, 2014 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psychobunny/9298141 to your computer and use it in GitHub Desktop.
Save psychobunny/9298141 to your computer and use it in GitHub Desktop.
middleware/index.js
var templates = require('./../../public/src/templates'),
translator = require('./../../public/src/translator'),
middleware = require('./middleware'),
meta = require('./../meta'),
db = require('./../database'),
auth = require('./../routes/authentication'),
async = require('async'),
path = require('path'),
fs = require('fs'),
nconf = require('nconf'),
express = require('express')
winston = require('winston');
/*
* Helpers
*/
function routeThemeScreenshots(app, themes) {
var screenshotPath;
async.each(themes, function(themeObj, next) {
if (themeObj.screenshot) {
screenshotPath = path.join(nconf.get('themes_path'), themeObj.id, themeObj.screenshot);
(function(id, path) {
fs.exists(path, function(exists) {
if (exists) {
app.get('/css/previews/' + id, function(req, res) {
res.sendfile(path);
});
}
});
})(themeObj.id, screenshotPath);
} else {
next(false);
}
});
}
function routeCurrentTheme(app, themeData) {
var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla');
// Detect if a theme has been selected, and handle appropriately
if (!themeData['theme:type'] || themeData['theme:type'] === 'local') {
// Local theme
if (process.env.NODE_ENV === 'development') {
winston.info('[themes] Using theme ' + themeId);
}
// Theme's static directory
if (themeData['theme:staticDir']) {
app.use('/css/assets', express.static(path.join(nconf.get('themes_path'), themeData['theme:id'], themeData['theme:staticDir']), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
if (process.env.NODE_ENV === 'development') {
winston.info('Static directory routed for theme: ' + themeData['theme:id']);
}
}
if (themeData['theme:templates']) {
app.use('/templates', express.static(path.join(nconf.get('themes_path'), themeData['theme:id'], themeData['theme:templates']), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
if (process.env.NODE_ENV === 'development') {
winston.info('Custom templates directory routed for theme: ' + themeData['theme:id']);
}
}
} else {
// If not using a local theme (bootswatch, etc), drop back to vanilla
if (process.env.NODE_ENV === 'development') {
winston.info('[themes] Using theme ' + themeId);
}
app.use(require('less-middleware')({
src: path.join(nconf.get('themes_path'), '/nodebb-theme-vanilla'),
dest: path.join(__dirname, '../../public/css'),
prefix: nconf.get('relative_path') + '/css',
yuicompress: app.enabled('minification') ? true : false
}));
}
}
function handleErrors(err, req, res, next) {
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
console.error(err.stack);
var status = err.status || 500;
res.status(status);
res.json(status, {
error: err.message
});
}
function catch404(req, res, next) {
var isLanguage = new RegExp('^' + nconf.get('relative_path') + '/language/[\\w]{2,}/.*.json'),
isClientScript = new RegExp('^' + nconf.get('relative_path') + '\\/src\\/forum(\\/admin)?\\/[\\w]+\\.js');
res.status(404);
if (isClientScript.test(req.url)) {
// Handle missing client-side scripts
res.type('text/javascript').send(200, '');
} else if (isLanguage.test(req.url)) {
// Handle languages by sending an empty object
res.json(200, {});
} else if (req.accepts('html')) {
// respond with html page
if (process.env.NODE_ENV === 'development') {
winston.warn('Route requested but not found: ' + req.url);
}
res.redirect(nconf.get('relative_path') + '/404');
} else if (req.accepts('json')) {
// respond with json
if (process.env.NODE_ENV === 'development') {
winston.warn('Route requested but not found: ' + req.url);
}
res.json({
error: 'Not found'
});
} else {
// default to plain-text. send()
res.type('txt').send('Not found');
}
}
module.exports = function(app, data) {
// Middlewares
app.configure(function() {
app.engine('tpl', templates.__express);
app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../../public/templates'));
// Pre-router middlewares
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico')));
app.use('/apple-touch-icon', middleware.routeTouchIcon);
app.use(require('less-middleware')({
src: path.join(__dirname, '../../', 'public'),
prefix: nconf.get('relative_path'),
yuicompress: app.enabled('minification') ? true : false
}));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
store: db.sessionStore,
secret: nconf.get('secret'),
key: 'express.sid',
cookie: {
maxAge: 1000 * 60 * 60 * 24 * parseInt(meta.configs.loginDays || 14, 10)
}
}));
app.use(express.csrf()); // todo, make this a conditional middleware
// Local vars, other assorted setup
app.use(function (req, res, next) {
res.locals.csrf_token = req.session._csrf;
// Disable framing
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
next();
});
app.use(middleware.processRender);
// Authentication Routes
auth.initialize(app);
routeCurrentTheme(app, data.currentThemeData);
// Route paths to screenshots for installed themes
routeThemeScreenshots(app, data.themesData);
app.use(app.router);
// Static directory /public
app.use(nconf.get('relative_path'), express.static(path.join(__dirname, '../../', 'public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
app.use(catch404);
app.use(handleErrors);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment