re-use your express routes with your websocket server.
define them once, use them everywhere.
npm install --save exprimus express engine.io cookie-parser express-session
first, install exprimus
npm install --save exprimus
npm install express
note that express is not mandatory; you could use exprimus as a pure socket router, without express.
Then, get a real-time framework, for example, engine.io
npm install --save engine.io
(for more info, head to Primus, which Exprimus uses internally)
if you want to share sessions with express, you'll have to get express-session and cookie-parser:
npm install --save cookie-parser express-session
var express =Exprimus =;var app =pApp =;pApp; //or whatever engine you're using
<script src="/primus/primus.js"> to your template, while testing. Later, you can use
pApp.save(path) to save the file somewhere.
From thereon, use pApp instead of app to define routes:
You can use
use, as normal.
From the client, you would do:
var primus = ;primus;//orprimus;//orprimus; //method defaults to getprimus;
If you want partial renders on the server side, you'll have to do a few things:
First, edit your layout, and make it conditional:
if (!skipLayout)doctype htmlhtmlheadscript(src="/primus/primus.js")bodyblock contentelseblock content
Primus augments the locals with a
skipLayout property; that way, you do not have to render the layout when serving a real-time request.
Then, use pApp instead or app to set the views settings:
Then just use res.render as normal:
on the client, the rendered template will be received in a property called
Provided you have installed
checkout the example:
git clone https://github.com/xananax/exprimus.git
cd exprimus/example && npm install
More or less. As long as you stick to the example and you don't have exotic needs, it should work.