Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorb committed Sep 27, 2011
1 parent c8fbb4c commit 20711cb
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 13 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.js
87 changes: 87 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
@@ -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).
33 changes: 33 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -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;

};
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"author": "Paul Vorbach <[email protected]> (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 <[email protected]> (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"
}
}
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});

0 comments on commit 20711cb

Please sign in to comment.