Skip to content

Commit

Permalink
init import
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc committed Sep 17, 2011
0 parents commit 07a79f2
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
bump.js
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2011 Kilian Ciuffolo, me@nailik.org

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
121 changes: 121 additions & 0 deletions README.md
@@ -0,0 +1,121 @@
# node-apiserver ![project status](http://dl.dropbox.com/u/2208502/maintained.png)

A ready to go modular http API Server.

#transports

- JSON
- JSONP,
- JSONP+iFrame _(you can make cross-domain POST inside the same parent domain: 's1.example.com, s2.example.com, ...')_

## Dependencies

- nodejs v0.4.11+
- http _native module_
- querystring _native module_
- node-bufferjoiner [repository](https://github.com/kilianc/node-bufferjoiner)

## Installation and first run

$ git clone git://github.com/kilianc/node-apiserver.git
$ cd node-apiserver
$ node example.js

## Usage

var util = require('util'),
querystring = require('querystring'),
ApiServer = require('node-apiserver'),
ApiModule = require('node-apiserver').ApiModule;

...

var UserModule = function(userCollection, myotherObj, myCustomConfig) {
...
this.userCollection = userCollection;
ApiModule.call(this);
};

util.inherits(UserModule, ApiModule);

// this is a private method, it means that you must send a right Authorization header within your request.
// your method can be reached to http://yourserver.com/user/my_first_camel_case_method
UserModule.prototype.myFirstCamelCaseMethodPrivate = function(request, response) {

var self = this;
var postData = this.parsePost(request); //inherited from ApiModule.

if(!/^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/.test(postData.email)){
self.emit('responseReady', request, response, 200, null, { success: false, reason: 'email not valid' });
return this;
}

this.userCollection.insert({ email: postData.email }, { safe: true }, function(err, user) {

if(err){
self.emit('responseReady', request, response, 200, 'this is the http status message', { success: false, reason: 'email not unique' });
return this;
}

self.emit('responseReady', request, response, 200, 'this is the http status message', { email: postData.email, success: true });
});
};

// this is a public method
// the server will check for the [Private|Public] word at the end of the method name.
// your method can be reached to http://yourserver.com/user/my_second_camel_case_method
UserModule.prototype.mySecondCamelCaseMethodPublic = function(request, response) {

var self = this;
var postData = this.parsePost(request);

if(!/^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/.test(postData.email)){
self.emit('responseReady', request, response, 200, null, { success: false, reason: 'email not valid' });
return this;
}

this.userCollection.insert({ email: postData.email }, { safe: true }, function(err, user) {

if(err){
self.emit('responseReady', request, response, 200, 'this is the http status message', { success: false, reason: 'email not unique' });
return this;
}

self.emit('responseReady', request, response, 200, 'this is the http status message', { email: postData.email, success: true });
});
};

...

var apiServer = new ApiServer('mydomain.com', standardHeaders, "username:password");
apiServer.addModule('user', new UserModule(new mongodb.Collection(mongodbClient, 'users'), { foo: 'bar' }, { cool: 'sure' }));
apiServer.listen(80);

...

## License

_This software is released under the MIT license cited below_.

Copyright (c) 2010 Kilian Ciuffolo, me@nailik.org. All Rights Reserved.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions lib/apimodule.js
@@ -0,0 +1,44 @@
var util = require('util'),
events = require('events'),
querystring = require('querystring');

var ApiModule = function(mongodbClient, collections) {

this.mongodbClient = mongodbClient;
this.collections = collections;

events.EventEmitter.call(this);
};

util.inherits(ApiModule, events.EventEmitter);

ApiModule.prototype.parseAuthorization = function(request, response){

var authorization;

if(!request.headers.authorization) {
this.emit('responseReady', request, response, 401, 'Authorization Required', { error: { type: 'ApiException', message: 'missing authorization' } }, { 'www-authenticate': 'Basic realm=\'Please Authenticate\''});
return false;
}

authorization = new Buffer(request.headers.authorization.replace('Basic ', ''), 'base64').toString('utf8');
authorization = authorization.split(':');

return { email: authorization[0], password: authorization[1] };
};

ApiModule.prototype.parsePost = function(request){

var postData;

try{
postData = querystring.parse(request.postData.toString('utf8'));
}
catch(e){
postData = {};
}

return postData;
};

module.exports = ApiModule;

0 comments on commit 07a79f2

Please sign in to comment.