0.0.23 • Published 9 years ago

yarf v0.0.23

Weekly downloads
3
License
AGPL-1.0
Repository
github
Last release
9 years ago

yarf

Yet Another Restful Framework

The framework is meant to be a very lightweight REST Framework to be used in applications that want to match the traditional Model Mapper View Controller (MMVC). It requires your application to have a certain folder structure:

MainAppFolder\
    |
    |---Controllers\
    |       |
    |       |---controllerName.js
    |       |---controllerName1.js
    |---Mappers\
    |       |
    |       |---mapperName.js
    |       |---mapperName1.js
    |---Models\
    |       |
    |       |---modelName.js
    |---public\
    |       |
    |       |--file.html
    |       |--folder\
    |       |    |---file.jpg (or any other format)
  • Controller - contains the actions preceded by their HTTP Verbs. For example:
var constructor = function () {
    this.usersMapper = new(require('../Mappers/users.js'))();
    this.userModelClass = require('../Models/user.js');
}.extends(require('yarf').Controller);

constructor.prototype.postLogin = function () {
    if(typeof this._PAYLOAD['email'] == "undefined" || typeof this._PAYLOAD['password'] == "undefined"){
        this.statusCode = 404;
        this.response = undefined;
        return this.end();
    }
    this.usersMapper.login(this._PAYLOAD['email'], this._PAYLOAD['password'], function(err, doc){
        if(err){
            this.statusCode = 500; // something happened in the db server?!
        }else {
            if (doc == null || doc == undefined) {
                console.log('Login attempt failed from ', this.remoteIP, ":", this.remotePort , " with payload: ", this._PAYLOAD);
                this.response = undefined;
                this.statusCode = 404;
            }else{
                this._SESSION['login'] = doc._id.toString();
                this.response = doc;
                this.statusCode = 200;
            }
        }
        this.end();
    }.bind(this));
};

module.exports = constructor;

Controllers expose several properties:

Property NameWhat it does
this._SESSIONcontains all the session data that has been added to it in previous calls
this._GETcontains all query string vars: http://yoursite.tld/user/processGetVars?getVar1=300 will produce this._GET['getVar1'] with the value of 300
this._POSTcontains all form fields sent through POST
this._FILEScontains a list of files and where they are stored on the disk currently (temporary store), as well as their details as sent by the client
this._URLPARAMSan array with the params sent by url: http://site.tld/user/action/paramValue1/paramValue2 will produce ['paramValue1', 'paramValue2']
this._PAYLOADcontains the payload as received. If Content-Type was set to application/json then payload will contain the parsed object
this.remoteIpip of the remote (for now it respects directly the X-Forwarded-For header, future versions will make it so it respects only from trusted list of ips)
this.remotePortthe port of the remote connection. (when proxied the value may be inacurate)

Controllers also expose the following methods:

Method NameWhat it does
this.setCookiefunction(cookieName, cookieValue, options) Sets a cookie with the cookieValue value and cookieName name. Can add standard cookie options
this.endcall this when you're done with your request and the system can send the data to the user.
  • Mapper - Contains data access layer. Will be used directly by the Controller. It has a this.db pointing to the current MongoDB connection An example:
var constructor = function () {
    Object.defineProperty(this, 'collection', {
        value: this.db.collection("users")
    });
    this.collection.ensureIndex({
        email: 1
    }, {unique: 1}, function (err) {
        if (err)throw err
    });
    this.collection.ensureIndex({
        email: 1,
        password: 1
    }, function (err) {
        if (err) throw err;
    });
}.extends(require('yarf').Mapper);
constructor.prototype.login = function (email, password, cb) {
    this.collection.findOne({
        email: email,
        password: digestPassword(password)
    }, {
        password:0
    }, function (err, doc) {
        if(err) console.log('!!!!ERROR IN DB!!!!', err);
        cb(err, doc);
    });// no need to bind.
};
module.exports = constructor;
  • Model - Contains the model of the data. For example:
var constructor = function(passedObject){
    // todo complete this model with proper checks and everything.
    var email = "";
    var password="";
    var name = "";
    var surname = "";
    var dob = new Date();
    var phoneNumber = "";
    Object.defineProperties(this,{
        email:{
            enumerable: true,
            configurable: false,
            set: function(paramMail){
                // ensure the email is correct
                if(!emailTest.test(paramMail)){ // all these checks will be replaced by specs
                    throw "Not Email";
                }
                email = paramMail;
            },
            get: function(){
                return email;
            }
        },
        password: {
            enumerable: true,
            configurable: false,
            set: function(passParam){
                if(typeof passParam != "string" || passParam.length < 8 ){ // whatever other stuff you may want to use
                    throw "invalid password";
                }
                password = passParam;
            },
            get: function(){
                return password;
            }
        }
    });

    if(typeof passedObject != 'undefined'){
        for(var enumProperty in this){
            this[enumProperty] = passedObject[enumProperty];
        }
    }
}.extends(require('yarf').Model);
module.exports = constructor;

##Boilerplate Example app.js:

var yarf = require("yarf");
yarf.start(8000, __dirname+"/Server", {
    mongo:{
        url: 'mongodb://localhost/myApp'
    },
    session:{
        collName: "mySessionCollection",
        sessVarName: "sid"
    }
});

##Future Roadmap:

  • Interfaces with mySQL if need arises or other dbs.
  • Break the code a bit more from the Router.js eventually into more files
0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago