Apology Middleware
raw JSON →The `apology-middleware` package, at its current stable version 0.1.0, offers a lightweight middleware solution for Node.js web applications focused on serving custom HTML error pages, particularly for HTTP 404 'Not Found' responses. It's designed to integrate with older middleware stacks like Connect and Express, which adhere to the standard `(req, res, next)` middleware signature. Instead of relying on default server error messages, it enables developers to specify a custom HTML file to be served, providing a more consistent and branded user experience. The package was explicitly noted as being in early development upon its release and has not received any significant updates or new stable versions since 2016, indicating an abandoned status. Its key differentiator was providing a simple mechanism for custom 404 pages within prevalent Node.js middleware ecosystems of that era.
Common errors
error Error: ENOENT: no such file or directory, open '/path/to/4oh4.html' ↓
apology() is an absolute and correct path to an existing HTML file, e.g., path.join(__dirname, '404.html'). error TypeError: app.use is not a function (when using with modern Express) ↓
(err, req, res, next)). For 404s, a simple catch-all route at the end of your middleware chain can render a custom page. Warnings
breaking This package is effectively abandoned, with its last commit over 8 years ago. It targets extremely old Node.js versions (>=0.10.0) and uses deprecated patterns. It is not recommended for new projects or production use. ↓
gotcha The package was explicitly marked as 'early development' at version 0.1.0. This implies that the API might have been unstable or subject to breaking changes had development continued, and its current functionality should not be assumed to be production-ready or fully feature-complete. ↓
gotcha The `apology` middleware must be placed correctly in the middleware chain. If placed after other middleware that might handle or terminate the request (e.g., a static file server), it may not be reached for 404 errors. ↓
Install
npm install apology-middleware yarn add apology-middleware pnpm add apology-middleware Imports
- apology
const apology = require('apology-middleware');
Quickstart
const http = require('http');
const connect = require('connect');
const apology = require('apology-middleware');
const serveStatic = require('serve-static');
const path = require('path');
const fs = require('fs');
// Create a dummy 404 HTML file for demonstration
const custom404Path = path.join(__dirname, 'custom404.html');
fs.writeFileSync(custom404Path, '<h1>Custom 404 Not Found</h1><p>The page you requested could not be found.</p>');
const app = connect()
// Use apology middleware for 404s, pointing to the custom HTML file
.use(apology(custom404Path))
// Serve static files from the current directory (ensure / will work)
.use(serveStatic(__dirname));
const PORT = 3000;
const server = http.createServer(app).listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log('Try visiting http://localhost:3000/non-existent-page to see the custom 404.');
console.log('Serving a static file would work at http://localhost:3000/custom404.html');
});
// Clean up the dummy file on exit (optional)
process.on('exit', () => {
if (fs.existsSync(custom404Path)) {
fs.unlinkSync(custom404Path);
}
});