npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

exprimus

v0.5.2

Published

Binds Express and any real time engine through Primus

Downloads

11

Readme

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 = require('express')
,   Exprimus = require('exprimus')
;

var app = express()
,   pApp = Exprimus(app)
;
pApp.set('transformer','engine.io'); //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.get('/data/:id',function(req,res,next){
    var id = req.param('id');
    if(req.isSocket){ // this is a primus request
        res.send({title:id});
    }
    else{ // this is a normal request
        res.render('somepage',{title:id});
    }
})

You can use get, put, delete, post, and use, as normal.

From the client, you would do:

var primus = new Primus();
primus.write('/data/123');
//or
primus.write({url:'/data/123'});
//or
primus.write({url:'/data/123',method:'post'}); //method defaults to get

primus.on('data',function(data){
    console.log(data)    
});

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 content
else
    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.set('views', path.join(__dirname,'views'));
pApp.set('view engine', 'jade');

Then just use res.render as normal:

pApp.get('/',function(req,res){
    res.render('index',{title:'home'}); // will work for primus and express requests
});

on the client, the rendered template will be received in a property called render:

primus.on('data',function(data){
    if(typeof data !== 'string'){
        if(data.render){data = data.render;}
        else{data = '<pre>'+JSON.stringify(data)+'</pre>';}
    }
    document.getElementById('something').innerHTML = data;
});

Session persistence

pApp.set('secret','shhh, very secret');
pApp.set('store',new pApp.session.MemoryStore());
pApp.session();

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