node-apiserver
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
Installation as submodule
$ git clone git://github.com/kilian/node-apiserver.git
Installation with npm
$ npm install apiserver
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);
UserModule.prototype.myFirstCamelCaseMethodPrivate = 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 });
});
};
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.