You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

html-express-js

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-express-js

An Express template engine to render HTML views using native JavaScript

2.0.0
Source
npmnpm
Version published
Weekly downloads
17
-55.26%
Maintainers
1
Weekly downloads
 
Created
Source

build npm node

html-express-js

Features

  • Serves HTML documents using template literals
  • Supports includes in served HTML documents

Installation

npm install html-express-js

Basic Usage

The following shows at a high level 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, { staticIndexHandler } from 'html-express-js';

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

// set up engine
app.engine(
  'js',
  htmlExpress({
    includesDir: 'includes', // where all includes reside
  }),
);
// use engine
app.set('view engine', 'js');

// set directory where all index.js pages are served
app.set('views', `${__dirname}/public`);

// render HTML in public/homepage.js with data
app.get('/', function (req, res, next) {
  res.render('homepage', {
    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({
    viewsDir: `${__dirname}/public`, // root views directory to serve all index.js files
    notFoundView: '404/index', // relative to viewsDir above
  }),
);

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>
`;

Keywords

html

FAQs

Package last updated on 12 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts