diff --git a/.npmignore b/.npmignore new file mode 100644 index 000000000..daa602947 --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +test.js diff --git a/README.mkd b/README.mkd new file mode 100644 index 000000000..3386f19af --- /dev/null +++ b/README.mkd @@ -0,0 +1,87 @@ +**API** is a module for easily providing several RESTful APIs on a single +server. It inherits all methods from `http.Server`. + +## Usage + +`test.js`: + +```javascript +// Use the HTTP version +var api = require('api')('http'), + msg = 'Hello World!'; + +var server = new api.Server(); +server.listen(1337, '127.0.0.1'); + +function error404(res) { + res.writeHead(404); + res.end('Not found.\n'); +}; + +// Listen for regular requests and end with an 404 error. +server.on('regularRequest', function(req, res) { + error404(res); +}); + +// Listen for requests to /hello-world. +server.on('/hello-world', function(req, res) { + if (req.method == 'GET') { + res.writeHead(200); + res.end(msg+'\n'); + } else if (req.method == 'POST') { + res.writeHead(200); + msg = ''; + + // Update the message + req.on('data', function(chunk) { + msg += chunk; + }); + + // After the POST request has ended, end with the message. + req.on('end', function() { + res.end('Successfully changed message to "'+msg+'".\n'); + }); + } else { + error404(res); + } +}); +``` + +Test it. + +### Terminal 1 + +```bash +$ node test.js +``` + +### Terminal 2 + +```bash +$ curl http://localhost:1337/hello-world +Hello World! +$ curl -d foobar http://localhost:1337/hello-world +Successfully changed message to "foobar". +$ curl http://localhost:1337/hello-world +foobar +$ curl http://localhost:1337/ +Not Found. +``` + +## HTTP and HTTPS + +You can use HTTP or HTTPS if you want by providing the argument when including +the package. + +For HTTP do `require('api')('http')`. +For HTTPS do `require('api')('https')`. + +## Bugs and Issues + +If you encounter any bugs or issues, feel free to open an issue at +[github](//github.com/pvorb/node-api/issues). + +## License + +This package is licensed under the +[MIT license](http://vorb.de/license/mit.html). diff --git a/api.js b/api.js index e69de29bb..60ad133a2 100644 --- a/api.js +++ b/api.js @@ -0,0 +1,33 @@ +module.exports = function(version) { + +if (version !== 'http' && version !== 'https') + throw new Error(version + ' not supported.'); + +var util = require('util'), + http = require(version), + url = require('url'), + api = {}; + +// API Server inherits EventEmitter +function Server () { + if (!(this instanceof Server)) return new Server(); + http.Server.call(this); + + // Listen for any request and map the request to it's own event. + // 'regularRequest' is emitted, when there are no listeners for the event. + this.addListener('request', function(req, res) { + var path = url.parse(req.url).pathname; + + if (this.listeners(path).length > 0) + this.emit(path, req, res); + else + this.emit('regularRequest', req, res); + }); +}; +util.inherits(Server, http.Server); + +api.Server = Server; + +return api; + +}; diff --git a/package.json b/package.json index 5d6ce5185..083631937 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,18 @@ { - "author": "Paul Vorbach (https://vorb.de/)", - "name": "api", - "description": "A server for RESTful APIs", - "version": "0.0.0", - "repository": { - "type": "git", - "url": "git://github.com/pvorb/node-api.git" - }, - "main": "api.js", - "engines": { - "node": "*" - }, - "tags": [ "REST", "API" ] + "author": "Paul Vorbach (https://vorb.de/)", + "name": "api", + "description": "A server for RESTful APIs", + "tags": [ "REST", "API", "server" ], + "version": "0.0.0", + "repository": { + "type": "git", + "url": "git://github.com/pvorb/node-api.git" + }, + "bugs": { + "web": "https://github.com/pvorb/node-api/issues" + }, + "main": "api.js", + "engines": { + "node": ">=0.4.0" + } } diff --git a/test.js b/test.js new file mode 100644 index 000000000..3df943960 --- /dev/null +++ b/test.js @@ -0,0 +1,38 @@ +var api = require('./')('http'), + msg = 'Hello World!'; + +var server = new api.Server(); +server.listen(1337, '127.0.0.1'); + +function error404(res) { + res.writeHead(404); + res.end('Not found.\n'); +}; + +// Listen for regular requests and end with an 404 error. +server.on('regularRequest', function(req, res) { + error404(res); +}); + +// Listen for requests to /hello-world. +server.on('/hello-world', function(req, res) { + if (req.method == 'GET') { + res.writeHead(200); + res.end(msg+'\n'); + } else if (req.method == 'POST') { + res.writeHead(200); + msg = ''; + + // Update the message + req.on('data', function(chunk) { + msg += chunk; + }); + + // After the POST request has ended, end with the message. + req.on('end', function() { + res.end('Successfully changed message to "'+msg+'".\n'); + }); + } else { + error404(res); + } +});