Skip to content

Instantly share code, notes, and snippets.

@LazyFatArrow
Created March 19, 2017 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LazyFatArrow/0c93aad793c0f262011991761e7e99ed to your computer and use it in GitHub Desktop.
Save LazyFatArrow/0c93aad793c0f262011991761e7e99ed to your computer and use it in GitHub Desktop.
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message
});
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment