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 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-express-js

v3.1.0

Published

An Express template engine to render HTML views using native JavaScript

Downloads

160

Readme

build npm node

html-express-js

Features

  • Serves HTML documents using template literals
  • Supports includes in HTML documents
  • Allows shared global state throughout templates

Installation

npm install html-express-js

Basic Usage

The following is a high level example of how the package can be used as an Express template engine. See example directory for all details of a working implementation.

Set up your Express app to use this engine:

import htmlExpress, { renderView } from 'html-express-js';

const app = express();
const __dirname = resolve();

const viewsDir = `${__dirname}/public`;

const { engine, staticIndexHandler } = htmlExpress({
  viewsDir, // root views directory to serve all index.js files
  includesDir: `${viewsDir}/includes`, // OPTIONAL: where all includes reside
  notFoundView: '404/index', // OPTIONAL: relative to viewsDir above
});

// set up engine
app.engine('js', engine);

// use engine
app.set('view engine', 'js');

// set directory where all index.js pages are served
app.set('views', viewsDir);

// render HTML in public/homepage.js with data
app.get('/', function (req, res, next) {
  renderView('homepage', req, res, {
    title: 'Awesome Homepage',
    name: 'Bob',
  });
});

// OPTIONALLY: route all GET requests to directories
// to their associated static index.js views in the public directory
// and, if not found, route to the 404/index.js view
app.use(staticIndexHandler());

Then you can create the associated files:

// public/includes/head.js
import { html } from 'html-express-js';

export const view = () => html`
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta
    name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0"
  />
`;
// public/homepage.js
import { html } from 'html-express-js';

export const view = (data, state) => html`
  <!doctype html>
  <html lang="en">
    <head>
      ${state.includes.head}
      <title>${data.title}</title>
    </head>

    <body>
      <h1>This is the homepage for ${data.name}</h1>
    </body>
  </html>
`;

Advanced usage

Injecting and using state based on a request

The following shows an example of showing a logged out state based on the cookie on a request.

import htmlExpress, { renderView } from 'html-express-js';

const app = express();
const __dirname = resolve();

const viewsDir = `${__dirname}/public`;

const { engine, staticIndexHandler } = htmlExpress({
  viewsDir,
  /**
   * Inject global state into all views based on cookie
   */
  buildRequestState: (req) => {
    if (req.cookies['authed']) {
      return {
        loggedIn: true,
      };
    }
  },
});

app.engine('js', engine);
app.set('view engine', 'js');
app.set('views', viewsDir);

app.get('/', function (req, res, next) {
  renderView('homepage', req, res);
});
// public/homepage.js
import { html } from 'html-express-js';

export const view = (data, state) => {
  const { loggedIn } = state;

  return html`
    <!doctype html>
    <html lang="en">
      <head>
        <title>${data.title}</title>
      </head>

      <body>
        ${loggedIn ? `<a href="/logout">Logout</a>` : 'Not logged in'}
      </body>
    </html>
  `;
};

Development

Run site in examples directory

npm start

Run tests

npm test