express-play

0.0.4 • Public • Published

express-play

Express based MVC Framework for Node.js

This project is under construction. Don't use it yet.

File Structure Based

The default file structure and the entry file of an express-play application:

/app
  /controllers     - controllers root directory
    /contents      - controllers sub directory
      articles.js  - articles controller (/contents/articles/... auto-loaded)
    accounts.js    - account controller (/accounts/... auto-loaded)
    home.js        - home controller (/home/... auto-loaded)
  /lib             - modules root directory
    logger.js      - logger module (auto-loaded and auto-injected)
    repository.js  - repository module (auto-loaded and auto-injected)
server.js
// server.js
// Super simple, isn't it?
require('express-play').play(3000);

The HTTP Request Handler Routing

The framework maps resource handler routings automatically using the object graph, function names and 'id' parameter that is special in express-play.

// /app/controllers/contents/articles.js
function ArticlesController() {
  // GET /contents/articles
  // GET /contents/articles/:id
  this.get = function (id) {
  };
 
  // POST /contents/articles
  this.post = function () {
  };
 
  this.comments = {
    // GET /contents/articles/comments
    // GET /contents/articles/comments/:id
    get: function (id) {
    },
    
    // POST /contents/articles/comments
    post: function () {
    }
  };
 
  // GET /contents/articles/top
  this.top = function () {
  };
}
 
module.exports = ArticlesController;

Query as Parameters

Query values are injected to the handler function as parameters automatically. The order of parameters is not important.

// /app/controllers/contents/articles.js
function ArticlesController() {
  // GET /contents/articles/top?by=[by]&count=[count]
  this.top = function (by, count) {
    by = by || 'likes';
    count = count || 10;
  };
}
 
module.exports = ArticlesController;

Dependency Injection

An object module:

// /app/lib/logger.js
function Logger() {
}
 
// The module name is 'logger' that is inferred from the file name.
module.exports = new Logger();

A constructor module:

// app/lib/repository.js
function Repository() {
}
 
// The module name is 'Repository' that is the constructor function name.
module.exports = Repository;

Injection to Controllers

// /app/controllers/contents/articles.js
function ArticlesController(logger) {
  // 'logger' is injected to this controller once.
  this.logger = logger;
}
 
module.exports = ArticlesController;

Injection to Handler Functions

// /app/controllers/contents/articles.js
function ArticlesController() {
  // GET /contents/articles/top
  this.top = function (Repository) {
    // A new instance of 'Repository' is injected to this function everytime the handler is called.
    var repo = Repository;
  };
}
 
module.exports = ArticlesController;

License

The MIT License (MIT)

Copyright (c) 2014 Yi Gyuwon gyuwon@live.com

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.

Readme

Keywords

none

Package Sidebar

Install

npm i express-play

Weekly Downloads

1

Version

0.0.4

License

MIT

Last publish

Collaborators

  • gyuwon