node package manager

tapi

Tiny RESTful API frameworks bases on express.js

Tiny RESTful API frameworks - TAPI

  • This is a tiny frameworks for RESTful API applications.
  • TAPI bases on express.js.
  • Designed for applications with express and mongoose, but not depends on mongoose.
npm isntall tapi
-lib
|-controllers
 |-Users.js
|-medols
 |-Users.js
-app.js
var express = require('express')
, tapi = require('tapi')
, mongoose = require('mongoose')
, app = express();

mongoose.connect('mongodb://user:pw@localhost/test');

tapi.config({
    libs: './lib',
    prefix: '/api/version-1/'
});

app.use(app.router);
app.api('/user/:id?', 'user.show', 'GET');
app.listen(3000);
exports.show = function(req, res, next) {
    this.send(200, this.medol);
};
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, UsersSchema = new Schema({
    nickname: String
});

module.exports = mongoose.model('Users', UsersSchema);
{
    "request": "/api/version-1/user/1234",
    "code": 200,
    "message": "OK",
    "data": {
        "nickname": "XXXXXX"
        "_id": [57, 56 ...]
    }
}
tapi.config({
    libs: String,  //dir path of controllers and medols, default "./lib"
    prefix: String //prefix for the dir path of route, default "/"
})
app.api('/user/:id?', 'Users'); // only controller, default action mapping will be used, see `Actions Mapping` below.
app.api('/posts/:id', 'Posts.show') // no HTTP method, default method 'all'(app.all()) will be used.
app.api('/posts/new', 'Posts.create', 'post');
  • default function for all controllers
  • will format the 'response' data before res.send(), and will call res.send() automatically
  • support ‘JSON' and 'XML' format, see Format below

Samples:

this.send('All done.'); // default code '200', default message see `Default HTTP Status` below
this.send(404, 'Nothing!');
this.send(500, 'Errors', 'Wrong URL!')
{
	index: 'get',
	latest: 'get',
	create:  'post',
	get: 'get',
	update: 'put',
	patch: 'patch',
	del: 'del'
}
format = req.body.format || req.params.format || 'json';
{
	'200': 'OK',
	'201': 'Created',
	'500': 'Internal Server Error. -- Unkown errors.',
	'401': 'Unauthorized. -- Need to sign in.',
	'402': 'Payment Required. -- Please pay your orders.',
	'403': 'Forbidden. -- No permission to get the response from this request.',
	'404': 'Not Found. -- Can not find your require path.',
	'454': 'Unavailable Method. -- No such method within the api.'
}