A universal (backend and frontend) application framework based on Express.js.
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();
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 viapageFactory
, and registered withroutes
. Page directories atdir
must contain aserver.js
file, and may optionally contain alocales
subdirectory, atemplates
subdirectory, and asettings.js
file.locales: { load: (dirpath) => void }
: if alocales
directory exists in the applicationdir
or in a pagedir
, the resolved path will be passed to theload
method. Thelocales
object can be retrieved viaapp.get('locales')
.templates: { load: (dirpath) => void }
: if atemplates
directory exists in the applicationdir
or in a pagedir
, the resolved path will be passed to theload
method. Thetemplates
object can be retrieved viaapp.get('templates')
.settings: { set: (key: String, value: any) }
: if asettings.js
file exists in the applicationdir
or in a pagedir
, the file content will be stored under theid
key. Thesettings
object can be retrieved viaapp.get('settings')
.middleware: { register: (app) => void, registerError(app) => void }
: middleware registration hooks. Theregister
method will be invoked before, andregisterError
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
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 viapageFactory
, and registered withroutes
.middleware: { register: (app) => void, registerError(app) => void }
: middleware registration hooks. Theregister
method will be invoked before, andregisterError
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
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)