12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- var express = require('express');
- var path = require('path');
- var logger = require('morgan');
- var compression = require('compression');
- var methodOverride = require('method-override');
- var session = require('express-session');
- var flash = require('express-flash');
- var bodyParser = require('body-parser');
- var expressValidator = require('express-validator');
- var dotenv = require('dotenv');
- var nunjucks = require('nunjucks');
- var markdown = require('nunjucks-markdown');
- var marked = require('marked');
- var mongoose = require('mongoose');
- var passport = require('passport');
- var routes = require('./routes');
- // Load environment variables from .env file
- dotenv.load();
- // Passport OAuth strategies
- require('./config/passport');
- var app = express();
- mongoose.connect(process.env.DB_PATH);
- mongoose.connection.on('error', function() {
- console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
- process.exit(1);
- });
- // view engine setup
- var njks = nunjucks.configure('views', {
- autoescape: true,
- express: app,
- marked: true
- });
- markdown.register(njks, marked);
- app.set('view engine', 'html');
- app.set('port', process.env.PORT || 3999);
- app.use(compression());
- app.use(logger('dev'));
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(expressValidator());
- app.use(methodOverride('_method'));
- app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
- app.use(flash());
- app.use(passport.initialize());
- app.use(passport.session());
- app.use(function(req, res, next) {
- res.locals.user = req.user;
- next();
- });
- app.use(express.static(path.join(__dirname, 'public')));
- /**
- * Setup Routes
- */
- routes.init(app, passport);
- uploadDir = path.join(__dirname, '/public/media/');
- // Production error handler
- if (app.get('env') === 'production') {
- app.use(function(err, req, res, next) {
- console.error(err.stack);
- res.sendStatus(err.status || 500);
- });
- }
- app.listen(app.get('port'), function() {
- console.log('Express server listening on port ' + app.get('port'));
- });
- module.exports = app;
|