0.3.1 • Published 9 years ago

revres v0.3.1

Weekly downloads
1
License
MIT
Repository
-
Last release
9 years ago

REVRES

An incredibly minimal REST api server.

Installation

$ npm install --save revres

Usage

Importing

var revres = require('revres');

Creating a servlet

var servlet = revres([port][, callback]);

Setting a REST endpoint

servlet.get(path, callback);
  • path: string (javascript regex allowed, in string format)
  • callback: function

The callback is called when a request to the given path happens.

Callback params:

  • req: object containing parsed request url data (see node url docs)
  • send: function that takes two parameters (response, content-type)

(Also works with post)

Synchronous

servlet.getSync(path, callback)
  • path: string (javascript regex allowed, in string format)
  • callback: function

Callback params:

  • req: object containing parsed request url data (see node url docs)

When it is time to send a response, just return a string or json object and it will be sent to the client

(Also works with post)

Example

// Create a servlet on port 5000
var servlet = revres(5000);

// Send response 'world' when /hello is requested
servlet.get('/hello', funciton (req, send) {
	send('world');
});

// Count to a number synchronously
servlet.getSync('/count-\\d{1,16}', function (req) {
	var count = parseInt(req.path.split('-')[1]);
	for (var i = 0; i < count; i++) {
		console.log(i);
		if (i > 20000) return 'early';
	}
	return 'done counting';
});

To Do

  • Allow user to pass in server object
0.3.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago

0.0.1

9 years ago