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.
- Host: GitHub
- URL: https://github.com/marko-js/express
- Owner: marko-js
- License: mit
- Created: 2020-08-13T19:25:04.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-11-03T22:53:27.000Z (over 2 years ago)
- Last Synced: 2025-04-26T00:03:36.825Z (about 2 months ago)
- Language: TypeScript
- Size: 337 KB
- Stars: 9
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
@marko/express
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.