npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

kensington-express

v1.0.7

Published

Express middleware for function-based view rendering with kensington

Readme

kensington-express

Express middleware that attaches res.renderView() to each response, for use with function-based (no template engine) view rendering.

Installation

npm install express kensington kensington-express

Usage

// views/layout.js
import t from 'kensington';

export default function layout(locals, page) {
  return t.htmlWithDocType({ lang: 'en' },
    t.head(
      t.title(locals.title),
    ),
    t.body(
      page(locals),
    ),
  );
}
// views/home.js
import t from 'kensington';

export default function homePage({ title, items }) {
  return t.main(
    t.h1(title),
    t.ul(items.map(item => t.li(item))),
  );
}
// app.js
import kensingtonView from 'kensington-express';
import layout from './views/layout.js';
import homePage from './views/home.js';

app.use(kensingtonView({ defaultLayout: layout }));

app.get('/', (req, res) => {
  res.renderView(homePage, { title: 'Home', items: ['foo', 'bar'] });
});

API

kensingtonView(options?)

Returns an Express middleware. Call once during app setup.

| Parameter | Type | Description | |---|---|---| | options.defaultLayout | LayoutRenderer \| null | Wraps every page renderer. Omit it or pass null to render pages without a layout. | | options.htmlValidator | (html: string) => void \| Promise<void> | Called after the response is sent. Useful for dev-time HTML linting. | | options.buildLocals | (req, res, options) => object | Replaces the default locals builder and returns the locals passed to both renderers. |

res.renderView(pageRenderer, options?)

Renders and sends an HTML response.

| Parameter | Type | Description | |---|---|---| | pageRenderer | (locals) => { toString(): string } | Renders the page content. Plain strings are supported. Called with this set to the response. | | options | object | Merged into locals. Pass layout to override the default layout for this response. |

By default, locals available to both renderers are merged in this order (later values win):

  1. req.route
  2. app.locals
  3. res.locals
  4. options passed to renderView

When buildLocals is provided, it receives (req, res, options) and its return value replaces this default merge.

Layouts and page renderers are both called with this set to the response, so a function renderer can read this.req. This also holds when layout: null or no defaultLayout is set. Arrow functions don't receive this; use buildLocals or res.locals to pass request data to them.

Layouts

To use an alternate layout for a route, pass it as layout:

// views/admin-layout.js
import t from 'kensington';

export default function adminLayout(locals, page) {
  return t.htmlWithDocType({ lang: 'en' },
    t.head(
      t.title(locals.title),
    ),
    t.body(
      t.nav('Admin'),
      page(locals),
    ),
  );
}
// app.js
import adminLayout from './views/admin-layout.js';

app.get('/admin', (req, res) => {
  res.renderView(adminPage, { layout: adminLayout, title: 'Admin' });
});

To skip the layout entirely for a route, pass layout: null:

res.renderView(myPageRenderer, { layout: null });

HTML validation

Install html-validate if you want to validate rendered markup during development:

npm install --save-dev html-validate

Pass an htmlValidator function to report markup issues. The response is sent before validation runs, so a validator that throws or rejects can't change the response. Once headers are sent, its error is logged with console.error instead of being passed to next():

import { HtmlValidate } from 'html-validate';

const htmlvalidate = new HtmlValidate();

async function htmlValidator(html) {
  const report = await htmlvalidate.validateString(html);
  if (!report.valid) console.warn(report.results);
}

app.use(kensingtonView({
  defaultLayout: layout,
  htmlValidator: process.env.NODE_ENV !== 'production' ? htmlValidator : undefined,
}));