Marko + Express
Quick Start
npm init marko -- --template vite-express
See the the express sample project for a working example.
From Scratch
First install Marko and the express related dependencies:
npm install marko @marko/express express --save
Skip the view engine
The built in view engine for express may be asynchronous, but it doesn't support streaming (check out Rediscovering Progressive HTML Rendering to see why this is so important). So instead we'll bypass the view engine and use @marko/express
.
Usage
The @marko/express
adds a res.marko
method to the express response object. This function works much like res.render
, but doesn't impose the restrictions of the express view engine and allows you to take full advantage of Marko's streaming and modular approach to templates.
By using res.marko
you'll automatically have access to req
, res
, app
, app.locals
, and res.locals
from within your Marko template and custom tags. These values are added to out.global
.
import express from "express"; import markoPlugin from "@marko/express"; import Template from "./template.marko"; const app = express(); app.use(markoPlugin()); //enable res.marko(template, data) app.get("/", function (req, res) { res.marko(Template, { name: "Frank", count: 30, colors: ["red", "green", "blue"] }); }); app.listen(8080);
BYOB (Bring your own bundler)
For the large portion of Marko's API a bundler is required. The example code above assumes that Marko templates can be loaded in your environment. Marko supports a number of bundlers, take a look through our supported bundlers and pick what works best for you.
EDITContributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.