Created
December 16, 2016 17:45
-
-
Save Bajix/038f95e319e20329ce5ae9fc3c83349d to your computer and use it in GitHub Desktop.
Sample DoneJS w/out SSR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var protectedDirs = require('./protected-directories'), | |
errorHandler = require('express-error-funnel'), | |
Handlebars = require('express-handlebars'), | |
validator = require('express-validator'), | |
handler = require('node-restify-errors'), | |
cookieParser = require('cookie-parser'), | |
session = require('./session').session, | |
compression = require('compression'), | |
bodyParser = require('body-parser'), | |
policies = require('./policies'), | |
Index = require('serve-index'), | |
passport = require('passport'), | |
winston = require('winston'), | |
express = require('express'), | |
routes = require('./routes'), | |
morgan = require('morgan'), | |
config = require('config'), | |
chalk = require('chalk'), | |
cors = require('./cors'), | |
path = require('path'); | |
var app = express(); | |
app.engine('stache', Handlebars({ | |
extname: '.stache', | |
defaultLayout: false, | |
helpers: { | |
is: function( a, b, options ) { | |
return a === b ? options.fn() : options.inverse(); | |
}, | |
joinBase: function() { | |
var args = Array.prototype.slice.call(arguments, 0, -1); | |
args.unshift('/assets'); | |
return path.join.apply(path, args); | |
} | |
} | |
})); | |
app.set('view engine', 'stache'); | |
app.set('views', path.join(__dirname, '../assets')); | |
app.use(compression()); | |
app.use(cors); | |
app.use('/node_modules', function( req, res, next ) { | |
var pathname = req.originalUrl; | |
for (var i = 0; i < protectedDirs.length; i++) { | |
if (~pathname.indexOf(protectedDirs[i])) { | |
return next(new handler.NotAuthorizedError()); | |
} | |
} | |
next(); | |
}); | |
if (config.env === 'development') { | |
app.use('/assets', Index(path.join(__dirname, '../assets'), { | |
hidden: true, | |
icons: true | |
})); | |
app.use('/node_modules', Index(path.join(__dirname, '../node_modules'), { | |
filter: function( filename, index, files, dir ) { | |
var pathname = path.join(dir, filename); | |
for (var i = 0; i < protectedDirs.length; i++) { | |
if (~pathname.indexOf(protectedDirs[i])) { | |
return false; | |
} | |
} | |
return true; | |
}, | |
hidden: true, | |
icons: true | |
})); | |
} | |
[ | |
'/css', | |
'/dist', | |
'/assets', | |
'/package.json', | |
'/node_modules' | |
].forEach(function( pathname ) { | |
app.use(pathname, express.static(path.join(__dirname, '../', pathname), { | |
fallthrough: false, | |
index: false | |
})); | |
}); | |
app.use(morgan('tiny', { | |
skip: function( req, res ) { | |
if (config.env === 'test') { | |
return true; | |
} | |
return false; | |
}, | |
stream: { | |
write: function( message ) { | |
winston.info(message); | |
} | |
} | |
})); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.use(validator()); | |
app.use(cookieParser(config.cookie.secret)); | |
app.use(session); | |
app.use(policies.bustHeaders); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(policies.cookieDomain); | |
app.use(policies.sessionExpire); | |
app.param('id', function( req, res, next ) { | |
req.assert('id', 'required').notEmpty(); | |
req.assert('id', 'objectID should be 24 characters long').len(24, 24); | |
req.assert('id', 'objectID should be hexidecimal').isHexadecimal(); | |
next(req.validationErrors()); | |
}); | |
routes.init(app); | |
app.use(function( req, res ) { | |
res.render('index'); | |
}); | |
app.use(function( err, req, res, next ) { | |
if (err instanceof Error && !err.statusCode || err.statusCode === 500) { | |
console.error(chalk.magenta(err.stack || err)); | |
} | |
next(err); | |
}); | |
app.use(errorHandler); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment