|
| 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 | +import mongoose from 'mongoose'; |
| 8 | + |
| 9 | +import index from './routes/index'; |
| 10 | +import users from './routes/users'; |
| 11 | +import dummyData from './dummyData'; |
| 12 | +import serverConfig from './config'; |
| 13 | + |
| 14 | +// Set native promises as mongoose promise |
| 15 | +mongoose.Promise = global.Promise; |
| 16 | + |
| 17 | +// MongoDB Connection |
| 18 | +mongoose.connect(serverConfig.mongoURL, (error) => { |
| 19 | + if (error) { |
| 20 | + console.error('Please make sure Mongodb is installed and running!'); // eslint-disable-line no-console |
| 21 | + throw error; |
| 22 | + } |
| 23 | + |
| 24 | + // feed some dummy data in DB. |
| 25 | + dummyData(); |
| 26 | +}); |
| 27 | + |
| 28 | +var app = express(); |
| 29 | + |
| 30 | +// view engine setup |
| 31 | +app.set('views', path.join(__dirname, 'views')); |
| 32 | +app.set('view engine', 'jade'); |
| 33 | + |
| 34 | +// uncomment after placing your favicon in /public |
| 35 | +//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); |
| 36 | +app.use(logger('dev')); |
| 37 | +app.use(bodyParser.json()); |
| 38 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 39 | +app.use(cookieParser()); |
| 40 | +app.use(express.static(path.join(__dirname, 'public'))); |
| 41 | + |
| 42 | +app.use('/', index); |
| 43 | +app.use('/', users); |
| 44 | + |
| 45 | +// catch 404 and forward to error handler |
| 46 | +app.use(function(req, res, next) { |
| 47 | + var err = new Error('Not Found'); |
| 48 | + err.status = 404; |
| 49 | + next(err); |
| 50 | +}); |
| 51 | + |
| 52 | +// error handler |
| 53 | +app.use(function(err, req, res, next) { |
| 54 | + // set locals, only providing error in development |
| 55 | + res.locals.message = err.message; |
| 56 | + res.locals.error = req.app.get('env') === 'development' ? err : {}; |
| 57 | + |
| 58 | + // render the error page |
| 59 | + res.status(err.status || 500); |
| 60 | + res.render('error'); |
| 61 | +}); |
| 62 | + |
| 63 | +module.exports = app; |
0 commit comments