0.16.0 • Published 8 years ago
complate-express v0.16.0
complate-express
Getting Started
Install complate-express in your Express application:
$ npm install complate-expressRegister complate's middleware, then use Response#complate for rendering:
let express = require("express");
let complate = require("complate-express");
let app = express();
// register complate middleware
app.use(complate("/path/to/views.js")));
app.get("/", (req, res) => {
res.complate("MyView", { title: "Hello World" });
});Views are typically generated from JSX modules:
import { createElement } from "complate-stream";
export function MyView({ title }) {
return <article>
<h1>{title}</h1>
<p>lorem ipsum dolor sit amet</p>
</article>;
}
// host API
export default (view, params, stream, fragment, callback) => {
return renderer.renderView(view, params, stream, { fragment }, callback);
};These JSX modules are then combined into a single views.js bundle, e.g. using
faucet - see
complate-sample-express
for details.
API
Request#complate(viewName, params, options)
viewNameidentifies the view within the bundleparamsis an object passed to the respective view macrooptionsis an optional object with the following members:fragment, iftrue, indicates that an HTML fragment (omitting doctype and layout)statusCodesets the HTTP status code (defaults to200)contentTypesets the corresponding HTTP response header (defaults to"text/html")
If Express's view cache is disabled, the bundle will be reloaded for each requests (useful for development).