Express JSON Prettifier Middleware

raw JSON →
0.1.2 verified Thu Apr 23 auth: no javascript abandoned

express-prettify is an Express.js middleware designed to automatically format (pretty-print) JSON responses based on the presence of a specified query parameter in the request URL. This enables developers to easily get human-readable API output for debugging or inspection by adding, for instance, `?pretty` to their requests, while standard minified JSON is returned otherwise. The current stable version is 0.1.2. However, the package has not seen significant updates since 2018, indicating an effectively abandoned status. It differentiates itself by offering a simple, client-controlled mechanism for JSON formatting without requiring server-side changes to every `res.json()` call or a global configuration.

error TypeError: Cannot read properties of undefined (reading 'call')
cause Attempting to `import` `express-prettify` in an ES Modules environment when the package is CommonJS-only.
fix
Use const pretty = require('express-prettify'); and ensure your environment is configured for CommonJS, or use a dynamic import() as a workaround: const pretty = (await import('express-prettify')).default;.
error JSON output is not pretty-printed even when the query parameter is present in the URL.
cause The configured query parameter does not match the URL, or the `express-prettify` middleware is not applied correctly or in the right order.
fix
Double-check that the query option in pretty({ query: 'yourParam' }) exactly matches the query parameter you're using in the URL (e.g., ?yourParam). Ensure app.use(pretty(...)) is called before any routes that send JSON responses.
error TypeError: app.use is not a function
cause The `express()` factory function was called without assigning its result (the Express application instance) to the `app` variable.
fix
Ensure your Express application instance is correctly initialized: const app = require('express')();
gotcha This package is CommonJS-only and incompatible with native ES Modules (ESM) environments. Direct `import` statements will fail.
fix For ESM projects, ensure your `package.json` specifies `"type": "commonjs"` or dynamically import with `const pretty = await import('express-prettify')` (though not officially supported and should be avoided if possible). Consider using an ESM-compatible alternative.
gotcha The `express-prettify` package appears to be abandoned, with no significant updates or maintenance since 2018. This implies potential security vulnerabilities, lack of new features, and incompatibility with newer Node.js or Express versions.
fix Consider evaluating alternative, actively maintained Express middleware for JSON pretty-printing if long-term support, security patches, or new features are required.
gotcha This middleware overrides `res.json()`. If other middleware in your application also modify `res.json()`, their order of execution matters and might lead to unexpected behavior or conflicts.
fix Ensure `express-prettify` is placed appropriately in your middleware chain, typically after body parsers but before any middleware that specifically relies on or modifies `res.json()` functionality.
npm install express-prettify
yarn add express-prettify
pnpm add express-prettify

Demonstrates how to integrate `express-prettify` middleware into an Express application, enabling pretty-printed JSON responses when the `pretty` query parameter is present in the URL, and regular minified JSON otherwise.

"use strict";

const express = require('express');
const app = express();
const pretty = require('express-prettify');

// Configure the middleware to pretty print when the 'pretty' query parameter is present
app.use(pretty({ query: 'pretty' }));

app.get('/', function(req, res) {
  res.json({ 
    hello: 'world',
    message: 'This is pretty printed JSON if ?pretty is in the URL',
    data: { id: 1, type: 'example', value: Math.random() }
  });
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log(`Try: curl http://localhost:${PORT}?pretty`);
  console.log(`Try: curl http://localhost:${PORT}`);
});