can-ssr
Server-side rendering for CanJS.
Server
To start a full server that hosts your application run:
npm install can-ssr
node_modules/.bin/can-serve --port 3030
In your application folder.
Available options:
- -p, --port - Set the port the server should run on
- -d, --develop - Also starts a live-reload server
- -p, --proxy - Proxy a local path (default:
/api
) to the given URL (e.g.http://api.myapp.com
) - -t, --proxy-to
- Set the proxy endpoint (default: /api
) - --proxy-no-cert-check - Turn off SSL certificate verification
Usage
can-ssr
can be used either as Express middleware or programatically.
Express Middleware
Use the provided middleware to add server-side rendering to an existing Express server:
var ssr = ; app;
The middleware includes a live-reload utility that can automatically refresh the cache for server-rendered responses. Use the liveReload
option to enable this feature:
app;
Note: Make sure the ssr middleware is the last middleware in the chain but before the error handler. Errors when rendering the application will be passed to your Express error handler. Error status codes (e.g. 404s or others set via appState.status()
) will be rendered with the application.
Use Programatically
var ssr = ;var render = ; ;
API
asset-register
A module used to register assets:
var register = ; ;
register(moduleName, type, assetMaker)
Register takes the moduleName to register, a type associated with it, and a function that when called returns an HTMLElement.
register(type, assetMaker)
If registering an asset not associated with a particular module, supply only the type and a function that when called returns an HTMLElement.
asset (helper)
When rendering in Node, a special asset
helper is included. This is used to append assets within your template:
\{{asset "css"}} ...
In this example, all CSS (either <style>
or <link>
elements depending on whether you are in development or production.
\{{asset type}}
Specify the type
to insert. Assets can be registered with asset-register
(docs above). Types provided natively by JavaScriptMVC:
- css: Inserts a
<style>
or<link>
- inline-cache: If using the can.AppMap will insert data fetched as part of the page lifecycle.
License
MIT