Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get errorhandler to work in Express 4 #16

Closed
rawaludin opened this issue Feb 4, 2016 · 7 comments
Closed

Can't get errorhandler to work in Express 4 #16

rawaludin opened this issue Feb 4, 2016 · 7 comments
Assignees
Labels

Comments

@rawaludin
Copy link

I'm new in this. I have this code:

    var express = require('express')
    var responseTime = require('response-time')
    var errorhandler = require('errorhandler')
    var app = express()

    // view engine
    app.set('view engine', 'jade')

    // middleware
    app.use(express.static('public'))
    app.use(responseTime())
    app.use(errorhandler({log: false}))

    app.get('/', function (req, res) {
      // res.render('index')
      fails()
    })

    app.listen(3000)

When I hit / I thought I should get the errorhandler in styled html, but it just shown like this:

screen shot 2016-02-04 at 9 06 23 am

What should I do to show the errorhandler html on the response??

@jordonias
Copy link

Your error handler should be after the routes. Also fails() is not defined, I think you're looking to use next() but it should be one of the arguments like function(req, res, next).

var express = require('express');
var errorHandler = require('errorhandler');

var app = express();

app.get('/', function(req, res, next) {
  return next(new Error('some error'));
});

app.use(errorHandler());

app.listen(3000);

It will also work if an error is thrown like:

var express = require('express');
var errorHandler = require('errorhandler');

var app = express();

app.get('/', function(req, res, next) {
  throw new Error('some error');
});

app.use(errorHandler());

app.listen(3000);

You'll want to review some of the api documentation, specifically:
http://expressjs.com/en/guide/using-middleware.html
http://expressjs.com/en/guide/error-handling.html

@Knighton910
Copy link

Yeah @rahmatawaludin, like @jordonias said, you always wanna define errors after you're routes.
And @jordonias i think that would be really cool, as discussed on gitter to do a pull request with more error handling examples. 👍

@jordonias
Copy link

At a bare minimum there should be a link in README.md to the error-handling guide in the expressjs.com docs.

@Knighton910
Copy link

True 💯

Are you gonna make that pull request, or shall I?

@jordonias
Copy link

You can start one if you'd like. I won't have time until later.

@Knighton910
Copy link

Yea, me as well, i'll have it in the pull request queue tonight.

@dougwilson
Copy link
Contributor

I'm going to close this issue since the question was ultimately answered :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants