examples/error/index.js JAVASCRIPT 54 lines View on github.com → Search inside
1'use strict'23/**4 * Module dependencies.5 */67var express = require('../../');8var logger = require('morgan');9var app = module.exports = express();10var test = app.get('env') === 'test'1112if (!test) app.use(logger('dev'));1314// error handling middleware have an arity of 415// instead of the typical (req, res, next),16// otherwise they behave exactly like regular17// middleware, you may have several of them,18// in different orders etc.1920function error(err, req, res, next) {21  // log it22  if (!test) console.error(err.stack);2324  // respond with 500 "Internal Server Error".25  res.status(500);26  res.send('Internal Server Error');27}2829app.get('/', function () {30  // Caught and passed down to the errorHandler middleware31  throw new Error('something broke!');32});3334app.get('/next', function(req, res, next){35  // We can also pass exceptions to next()36  // The reason for process.nextTick() is to show that37  // next() can be called inside an async operation,38  // in real life it can be a DB read or HTTP request.39  process.nextTick(function(){40    next(new Error('oh no!'));41  });42});4344// the error handler is placed after routes45// if it were above it would not receive errors46// from app.get() etc47app.use(error);4849/* istanbul ignore next */50if (!module.parent) {51  app.listen(3000);52  console.log('Express started on port 3000');53}

Code quality findings 6

Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var express = require('../../');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var logger = require('morgan');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var app = module.exports = express();
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var test = app.get('env') === 'test'
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
var test = app.get('env') === 'test'
Remove debugging statements or use a logging library
info correctness console-log
console.log('Express started on port 3000');

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.