Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
/ framework Public archive

A universal (backend and frontend) application framework based on Express.js

License

Notifications You must be signed in to change notification settings

YR/framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM Version Build Status

A universal (backend and frontend) application framework based on Express.js.

Usage

Server:

const framework = require('@yr/framework');

const app = framework('myapp', process.env.PORT, process.cwd(), {
  locales: {
    /* store locales files */
    load(dirpath)
  },
  templates: {
    /* store precompiled template files */
    load(dirpath)
  },
  middleware: {
    /* register middleware */
    register(app),
    /* register error middleware */
    registerError(app)
  },
  params: {
    /* register param validators */
    register(app)
  },
  pages: {
    home: {
      dir: 'pages/home',
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale']
    },
    blog: {
      dir: 'pages/blog',
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale/blog']
    }
  },
  settings: {
    /* store page specific settings */
    set(key, value)
  }
});

app.listen();

Client:

const framework = require('@yr/framework');

const app = framework('myapp', {
  middleware: {
    /* register middleware */
    register(app),
    /* register error middleware */
    registerError(app)
  },
  params: {
    /* register param validators */
    register(app)
  },
  pages: {
    home: {
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale']
    },
    blog: {
      pageFactory: (id, app) => /* return Page instance */ ,
      routes: ['/:locale/blog']
    }
  },
  settings: {
    /* store page specific settings */
    set(key, value)
  }
});

app.listen();

API - Server

framework(id: String, port: Number, dir: String, options: Object): Express

Framework factory returning an Express application instance.

All key/value pairs in options will be stored as Express application settings (retrievable via app.get(key). If present, the following keys will be specifically handled:

  • pages: { [id: String]: { dir: String, pageFactory: (id: String, app: Express) => Page, routes: Array<String> }}: hash of pages to initialise. Page instances will be created via pageFactory, and registered with routes. Page directories at dir must contain a server.js file, and may optionally contain a locales subdirectory, a templates subdirectory, and a settings.js file.
  • locales: { load: (dirpath) => void }: if a locales directory exists in the application dir or in a page dir, the resolved path will be passed to the load method. The locales object can be retrieved via app.get('locales').
  • templates: { load: (dirpath) => void }: if a templates directory exists in the application dir or in a page dir, the resolved path will be passed to the load method. The templates object can be retrieved via app.get('templates').
  • settings: { set: (key: String, value: any) }: if a settings.js file exists in the application dir or in a page dir, the file content will be stored under the id key. The settings object can be retrieved via app.get('settings').
  • middleware: { register: (app) => void, registerError(app) => void }: middleware registration hooks. The register method will be invoked before, and registerError will be invoked after, page handlers are registered.
  • params: { register: (app) => void }: param validation hook

framework.Page: Page Reference to the Page class

framework.request: Object Reference to the Express request prototype

framework.response: Object Reference to the Express response prototype

framework.static: Object Reference to the Express static middleware

API - Client

framework(id: String, options: Object): Express

Framework factory returning an Express-client application instance.

All key/value pairs in options will be stored as Express application settings (retrievable via app.get(key). If present, the following keys will be specifically handled:

  • pages: { [id: String]: { pageFactory: (id: String, app: Express) => Page, routes: Array<String> }}: hash of pages to initialise. Page instances will be created via pageFactory, and registered with routes.
  • middleware: { register: (app) => void, registerError(app) => void }: middleware registration hooks. The register method will be invoked before, and registerError will be invoked after, page handlers are registered.
  • params: { register: (app) => void }: param validation hook

framework.Page: Page Reference to the Page class

framework.request: Object Reference to the Express-client request prototype

framework.response: Object Reference to the Express-client response prototype

Page

The Page class describes the default behaviour of site pages.

constructor(id: String, app: Express): Page Create a Page instance.

app: Express Reference to the Express application instance.

id: String The page id string.

debug: Function A debug instance, namespaced with id.

initialised: Boolean Flag indicating whether init() has been called.

Pages are subject to the following lifecycle: init -> handle -> render -> unrender -> unhandle

init(req: Request, res: Response, done: Function)

handle(req: Request, res: Response, done: Function)

render(req: Request, res: Response, done: Function)

unrender(req: Request, res: Response, done: Function)

unhandle(req: Request, res: Response, done: Function)

Middleware

Response extensions

About

A universal (backend and frontend) application framework based on Express.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •