Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Updating for latest versions of Node, Express, and MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
cwbuecheler committed Apr 4, 2018
1 parent a021ba6 commit 27195c0
Show file tree
Hide file tree
Showing 8 changed files with 1,131 additions and 243 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Creating a Simple RESTful Web App with Node.js, Express, and MongoDB
#Creating a Simple RESTful Web App with Node.js, Express, and MongoDB

A complete sample project for Front-End developers teaching the basics of REST and using them to build an easy, fast, single-page web app.

Expand Down
56 changes: 19 additions & 37 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');

// Database
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest2');

var routes = require('./routes/index');
var users = require('./routes/users');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

Expand All @@ -32,39 +30,23 @@ app.use(function(req,res,next){
next();
});

app.use('/', routes);
app.use('/users', users);
app.use('/', indexRouter);
app.use('/users', usersRouter);

/// catch 404 and forwarding to error handler
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
next(createError(404));
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
// error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
Loading

0 comments on commit 27195c0

Please sign in to comment.