Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Minimal NodeJS RPC, Web API
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
README.md
package.json
rex.js

README.md

rex

Minimal Node JS RPC, post to Express-type server.

Server

var express = require('express');
var app = express();
app.use(express.bodyParser());

api = {
  servertime: function(){
    return Date();
  },
  echo: function(p){
    return {echo: p};
  },
  something: function(p, req, res){
    // I can access req and res
  },
  someasync: function(p, req, res){
    // I need to be asynchronous
    fs.readFile('some.txt', function(e, r){
      res.end(r);
    });
  }
};

require('remote-ex')(api, app);

app.get('/', function(r, s, n){
  s.render('index.html', {});
})
app.listen(80);

index.html

// rex.js is generated by the server
<script src="rex.js"></script>

// call server functions
<script>
   rex.servertime();
   rex.echo('a=1&b=2');
   rex.echo({a: 1, b: 2});
</script>

Rex lets you call server functions from the browser. It has minimal footprint and works with Express, or other web servers that implement Express functions like .get(), .post(), and req.params.

Curl

$ curl --data "" servername/rex/servertime
$ curl --data "q=1" servername/rex/echo
  => {echo: {"q":"1"}}

or /api if you prefer..

$ curl --data "" servername/api/servertime
$ curl --data "q=1" servername/api/echo
  => {echo: {"q":"1"}}

Handle files

api = {
  myfunc: function(p, r, s){
    console.log(r.files);
  }
};

$ curl -F fn=@file.txt localhost/rex/file

Authentication

Rex is authentication and authorization agnostic, so you're free to select from your options. Then you can authorize in your functions, for instance if the client is providing a cookie token.

api = {
    myfunc: function(p, r, s){
       console.log(r.cookies); // requires cookie handler in Express
       if(r.cookies && r.cookies.token === 'a valid token'){
          return 'yr logged in';
       } else {
          return 'not authorized';
       }
    }
};

Why?

Page sizes, frameworks have gone nuts in size and browser load. Rex is simple to use. Rex uses only XMLHttp and POST, for maximum browser coverage, and may be especially useful for mobile devices or limited resource environments. There are no server-side requirements except for the web server.

Something went wrong with that request. Please try again.