Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | var express = require('express'); |
| 2 | var path = require('path'); |
| 3 | var favicon = require('serve-favicon'); |
| 4 | var logger = require('morgan'); |
| 5 | var cookieParser = require('cookie-parser'); |
| 6 | var bodyParser = require('body-parser'); |
| 7 | var serveIndex = require('serve-index'); |
| 8 | var serveStatic = require('serve-static') |
| 9 | |
| 10 | var routes = require('./routes/index'); |
| 11 | var users = require('./routes/users'); |
| 12 | var dgeusers = require('./dgeusers.json'); |
| 13 | console.log("Read these dgeusers: " + JSON.stringify(dgeusers)); |
| 14 | |
| 15 | var app = express(); |
| 16 | |
| 17 | // view engine setup |
| 18 | app.set('views', path.join(__dirname, 'views')); |
| 19 | app.set('view engine', 'ejs'); |
| 20 | |
| 21 | // uncomment after placing your favicon in /public |
| 22 | //app.use(favicon(__dirname + '/public/favicon.ico')); |
| 23 | app.use(logger('dev')); |
| 24 | app.use(bodyParser.json()); |
| 25 | app.use(bodyParser.urlencoded({ extended: false })); |
| 26 | app.use(cookieParser()); |
| 27 | app.use(express.static(path.join(__dirname, 'public'))); |
| 28 | |
| 29 | |
| 30 | // set up static serving and directory listings for each user's lib/flows directory |
| 31 | dgeusers.forEach( function(dgeuser) { |
| 32 | console.log("Setting up static serving for " + dgeuser); |
| 33 | var sDir = "../users/" + dgeuser + '/flows/shared'; |
| 34 | console.log("sDir" + sDir); |
| 35 | app.use('/listFlows/'+dgeuser+'/flows/shared', serveIndex(sDir, {'view': 'details', 'icons': true})) |
| 36 | }); |
| 37 | |
| 38 | app.use('/', routes); |
| 39 | app.use('/users', users); |
| 40 | |
| 41 | // catch 404 and forward to error handler |
| 42 | app.use(function(req, res, next) { |
| 43 | var err = new Error('Not Found'); |
| 44 | err.status = 404; |
| 45 | next(err); |
| 46 | }); |
| 47 | |
| 48 | // error handlers |
| 49 | |
| 50 | // development error handler |
| 51 | // will print stacktrace |
| 52 | if (app.get('env') === 'development') { |
| 53 | app.use(function(err, req, res, next) { |
| 54 | res.status(err.status || 500); |
| 55 | res.render('error', { |
| 56 | message: err.message, |
| 57 | error: err |
| 58 | }); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | // production error handler |
| 63 | // no stacktraces leaked to user |
| 64 | app.use(function(err, req, res, next) { |
| 65 | res.status(err.status || 500); |
| 66 | res.render('error', { |
| 67 | message: err.message, |
| 68 | error: {} |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | |
| 73 | module.exports = app; |