examples/error-pages/index.js JAVASCRIPT 104 lines View on github.com → Search inside
1'use strict'23/**4 * Module dependencies.5 */67var express = require('../../');8var path = require('node:path');9var app = module.exports = express();10var logger = require('morgan');11var silent = process.env.NODE_ENV === 'test'1213// general config14app.set('views', path.join(__dirname, 'views'));15app.set('view engine', 'ejs');1617// our custom "verbose errors" setting18// which we can use in the templates19// via settings['verbose errors']20app.enable('verbose errors');2122// disable them in production23// use $ NODE_ENV=production node examples/error-pages24if (app.settings.env === 'production') app.disable('verbose errors')2526silent || app.use(logger('dev'));2728// Routes2930app.get('/', function(req, res){31  res.render('index.ejs');32});3334app.get('/404', function(req, res, next){35  // trigger a 404 since no other middleware36  // will match /404 after this one, and we're not37  // responding here38  next();39});4041app.get('/403', function(req, res, next){42  // trigger a 403 error43  var err = new Error('not allowed!');44  err.status = 403;45  next(err);46});4748app.get('/500', function(req, res, next){49  // trigger a generic (500) error50  next(new Error('keyboard cat!'));51});5253// Error handlers5455// Since this is the last non-error-handling56// middleware use()d, we assume 404, as nothing else57// responded.5859// $ curl http://localhost:3000/notfound60// $ curl http://localhost:3000/notfound -H "Accept: application/json"61// $ curl http://localhost:3000/notfound -H "Accept: text/plain"6263app.use(function(req, res, next){64  res.status(404);6566  res.format({67    html: function () {68      res.render('404', { url: req.url })69    },70    json: function () {71      res.json({ error: 'Not found' })72    },73    default: function () {74      res.type('txt').send('Not found')75    }76  })77});7879// error-handling middleware, take the same form80// as regular middleware, however they require an81// arity of 4, aka the signature (err, req, res, next).82// when connect has an error, it will invoke ONLY error-handling83// middleware.8485// If we were to next() here any remaining non-error-handling86// middleware would then be executed, or if we next(err) to87// continue passing the error, only error-handling middleware88// would remain being executed, however here89// we simply respond with an error page.9091app.use(function(err, req, res, next){92  // we may use properties of the error object93  // here and next(err) appropriately, or if94  // we possibly recovered from the error, simply next().95  res.status(err.status || 500);96  res.render('500', { error: err });97});9899/* istanbul ignore next */100if (!module.parent) {101  app.listen(3000);102  console.log('Express started on port 3000');103}

Code quality findings 9

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 path = require('node:path');
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 logger = require('morgan');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var silent = process.env.NODE_ENV === 'test'
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
var silent = process.env.NODE_ENV === 'test'
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
if (app.settings.env === 'production') app.disable('verbose errors')
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var err = new Error('not allowed!');
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.