An open API service indexing awesome lists of open source software.

https://github.com/marko-js/express

Render Marko templates in an express application.
https://github.com/marko-js/express

Last synced: about 2 months ago
JSON representation

Render Marko templates in an express application.

Awesome Lists containing this project

README

        





@marko/express



TypeScript



Styled with prettier



Build status







NPM Version



Downloads

Render [Marko](https://markojs.com/) templates in an [`express`](http://expressjs.com/) application.

# Installation

```console
npm install @marko/express
```

# Examples

## Setup

```javascript
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`](https://markojs.com/docs/rendering/#global-data) is automatically merged with [`app.locals`](http://expressjs.com/en/5x/api.html#app.locals) and [`res.locals`](http://expressjs.com/en/5x/api.html#res.locals) from [`express`](http://expressjs.com/). This makes it easy to set some global data via express middleware, eg:

_middleware.js_

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

Then later in a template access via:

```marko

${out.global.locale}

```

## Redirects

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

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

```js
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="...">
});
```

# Code of Conduct

This project adheres to the [eBay Code of Conduct](./.github/CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.