@marko/express
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published


@marko/express
TypeScript Styled with prettier Build status NPM Version Downloads

Render Marko templates in an express application.

Installation

npm install @marko/express

Examples

Setup

import express from "express";
import markoMiddleware from "@marko/express";
import Template from "./template.marko";

const app = express();

app.use(markoMiddleware());

app.get("/", (req, res) => {
  // Streams Marko template into the response.
  // Forwards errors into expresses error handler.
  res.marko(Template, { hello: "world" });
});

$global / out.global

When calling res.marko the input.$global is automatically merged with app.locals and res.locals from express. This makes it easy to set some global data via express middleware, eg:

middleware.js

export default (req, res, next) => {
  res.locals.locale = "en-US";
};

Then later in a template access via:

<div>${out.global.locale}</div>

Redirects

Allows res.redirect to redirect HTML responses that have already begun sending content. This is done by flushing a <meta> tag redirect with a <script> fallback before prematurely ending the response.

If $global includes a cspNonce it will be included in the redirect script.

app.get("/", (req, res) => {
  res.marko(Template, { $global: { cspNonce: "xyz" } });

  // If a redirect occurs mid stream we'll see
  // something like the following in the output:
  //
  // <meta http-equiv=refresh content="0;url=...">
  // <script nonce="xyz">location.href="..."></script>
});

Code of Conduct

This project adheres to the eBay Code of Conduct. By participating in this project you agree to abide by its terms.

Readme

Keywords

Package Sidebar

Install

npm i @marko/express

Weekly Downloads

504

Version

2.1.0

License

MIT

Unpacked Size

25.1 kB

Total Files

9

Last publish

Collaborators

  • tigt
  • ryanturnquist
  • dylanpiercey
  • ryansolid
  • mlrawlings
  • agliga