EXPRIMUS
re-use your express routes with your websocket server.
define them once, use them everywhere.
Install
npm install --save exprimus express engine.io cookie-parser express-session
In details:
first, install exprimus
npm install --save exprimus
install express
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
Usage:
var express = Exprimus = ; var app = pApp = ;pApp; //or whatever engine you're using
Then, add <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:
pApp
You can use get
, put
, delete
, post
, and use
, as normal.
From the client, you would do:
var primus = ;primus;//orprimus;//orprimus; //method defaults to get primus;
Partial Renders
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 html html head script(src="/primus/primus.js") body block contentelse block 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:
pApp;pApp;
Then just use res.render as normal:
pApp;
on the client, the rendered template will be received in a property called render
:
primus;
Session persistence
pApp;pApp;pApp;
Provided you have installed cookie-parser
and express-session
.
Example:
checkout the example:
git clone https://github.com/xananax/exprimus.git
cd exprimus/example && npm install
npm start
Stable?
More or less. As long as you stick to the example and you don't have exotic needs, it should work.
License
MIT