-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |