0.2.0 • Published 9 years ago

comprest v0.2.0

Weekly downloads
4
License
ISC
Repository
github
Last release
9 years ago

CompREST

===

A productivity framework for Node.js REST applications. Student work for class - you've been warned.

####Example

var http = require('http');
var compRest = require('comprest');

var serverFunction = function(req, resp) {
  if ('/' === req.url) {
    compRest(resp).text('A sample server');
  }
  else if ('/json' === req.url) {
    var obj = { a: 'one', b: 'two' };
    compRest(resp).json(obj);
  }
  else if ('/download' === req.url) {
    var readFile = fs.createReadStream('./filename.png');
    compRest(resp).pipe(readFile);
  }
};

var server = http.createServer(serverFunction);
server.listen(3000);

####Functions

  • text(str, status) Expands to the following code:

    resp.writeHead((status? status : 200),
          {'Content-Type': 'text/plain'} );
    resp.write(str);
    resp.end();

    If status is not supplied, defaults to 200

  • json(obj, status) Expands to the following code:

    resp.writeHead((status? status : 200),
          {'Content-Type': 'application/json'} );
    resp.write(JSON.stringify(obj));
    resp.end();

    If status is not supplied, defaults to 200

  • pipe(stream, status) Expands to the following code:

    resp.writeHead((status? status : 200),
          {'Content-Type': 'text/plain'} );
    stream.pipe(resp);

    If status is not supplied, defaults to 200