exprest4
Yet another RESTful API framework for Express 4.x.
Install
$ npm install exprest4
Note that you have to npm install express
as well because exprest4
doesn't install Express 4.x by itself.
Getting started
Controllers
Make controllers
directory and code your Express application.
$ mkdir controllers$ vim app.js
// app.js 'use strict'; const express = exprest = app = ; exprest;
exprest4
regards each file/directory under the controllers
directory as a controller module in terms of MVC, and routes everything upon route()
call.
Each controller module must have the special property __exprest
as follow:
// controllers/example.js 'use strict'; moduleexports = __exprest: routes: action: 'list' action: 'view' path: ':id' action: 'create' method: 'post' action: 'update' path: ':id' method: 'put' action: 'remove' path: ':id' method: 'delete' { res; } { res; } { res; } { res; } { res; };
The code implemented in controllers/example.js
is equivalent to the following Express calls:
'use strict'; const example = ; app;app;app;app;app;
Models
If your application works with a database, exprest4
also allows you to define models by using Sequelize.
Make models
directory and add to call model()
as follows:
$ mkdir models$ vim app.js
exprest4
regards each file/directory under the models
directory as a model module in manner of Sequelize, and loads all models into a Sequelize instance upon model()
call.
// models/project.js 'use strict'; module { return sequelize;}; // app.js 'use strict'; const express = exprest = app = ; exprest // Use SQLite with memory storage;
model()
returns Promise
with a Sequelize instance.
It's a good idea to save the models as app.locals.models
as above code in app.js
, so that each controller can access to the models through req.app.locals.models
as the following code:
// controllers/project.js 'use strict'; { return reqapplocalsmodelsproject;} moduleexports = __exprest: routes: action: 'list' action: 'view' path: ':id' action: 'create' method: 'post' action: 'update' path: ':id' method: 'put' action: 'remove' path: ':id' method: 'delete' { ; } { ; } { ; } { ; ; } { ; ; };
Methods
route(app[, opts]) -> Promise
Routes controller modules for app
which must be an Express instance.
opts
is an object which may have the following properties:
-
controllers
: String [Default:path.join(process.cwd(), 'controllers')
]
Physical path to where controllers are implemented. -
url
: String [Default:'/'
]
Virtual path prefix for thecontrollers
. -
index
: String [Default:'index'
]
Controller's name which will be mapped directly ontourl
. -
authorizer
: Function [Default:undefined
]
Middleware for user authorization. -
templates
: Object [Default:{ crud: [...] }
]
Templates for routes.
Each controller module implemented in opts.controllers
directory will be mapped onto opts.url
.
You don't have to code for routing by yourself.
Just maintain APIs structure as physical directory structure.
If you call route()
once, which means you have only one controller directory, REST API is going to be flat like /api/<controllers>
.
You can also call route()
more than once to provide structured REST API.
For example, if you want to seperate APIs by version like /api/v1/<controllers>
and /api/v2/<controllers>
, create two controller directories controllers/v1
and controllers/v2
, then call route()
as follow:
// app.js 'use strict'; const path = express = exprest = app = api_versions = 1 2 controllers_dir = path; Promiseall api_versions;
If your REST APIs require users to be authenticated and/or to be authorized, opts.authorizer
is the right place to implement user authentication/authorization feature.
You can specifiy a middleware as opt.authorizer
to verify a user session, then it will be called at first for every controller.
The following example uses connect-ensure-login as opt.authorizer
:
// app.js 'use strict'; const path = express = exprest = app = ensureLoggedIn = ensureLoggedIn; exprest;
model([opts]) -> Promise
Imports models into a Sequelize instance.
opts
is an object which may have the following properties:
-
models
: String [Default:path.join(process.cwd(), 'models')
]
Physical path to where models are implemented. -
database
: String [Default:undefined
]
Database name. -
username
: String [Default:undefined
]
Username for database authentication. -
password
: String [Default:undefined
]
Password for database authentication.
Each model module implemented in opts.models
directory will be imported into a Sequelize instance after connection to a database is established.
The Sequelize instance will be returned with Promise
upon success.
If a model module has associate()
as a part of classMethods
, it will be called with sequelize.models
as its argument after all models are loaded.
So that it allows you to define associations between models.
opts.database
, opts.username
and opts.password
are passed to Sequelize's constructor.
Other options are also passed through to options
, the 4th arugment of Sequelize's constructor.
model.connect([opts]) -> Promise
Connects to a database through Sequelize by calling Sequelize.authenticate()
.
opts
is an object which may have the following properties:
-
database
: String [Default:undefined
]
Database name. -
username
: String [Default:undefined
]
Username for database authentication. -
password
: String [Default:undefined
]
Password for database authentication.
The Sequelize instance will be returned with Promise
upon success.
Each property of opts
is passed to Sequelize's constructor.
Other options are also passed through to options
, the 4th arugment of Sequelize's constructor.
model.sync([opts]) -> Promise
Synchronizes all defined models to a database.
opts
is an object which may have the following properties:
-
models
: String [Default:path.join(process.cwd(), 'models')
]
Physical path to where models are implemented. -
database
: String [Default:undefined
]
Database name. -
schema
: String [Default:undefined
]
Schema name. -
username
: String [Default:undefined
]
Username for database authentication. -
password
: String [Default:undefined
]
Password for database authentication.
The Sequelize instance will be returned with Promise
upon success.
opts.database
, opts.username
and opts.password
are passed to Sequelize's constructor.
Other options are also passed through to options
of both Sequelize's constructor and Sequelize.sync()
.
Note that this function is equivalent to Sequelize.sync()
unless Sequelize connects to PostgreSQL database.
There is an issue in Sequelize.sync()
for PostgreSQL, even if options.schema
is specified, no schema is prepended to table names while executing dropTable()
.
So that the tables under the public
schema with the same names as your models might be dropped unintentionally by Sequelize.sync()
.
This function provides workaround for the issue.
opts.schema
is passed to each model module implemented in opts.models
directory as the third argument.
The model modules have to pass the schema
as options.schema
to Sequelize.define()
as the following example:
// models/project.js 'use strict'; module { return sequelize;};
__exprest
Property
routes
: Array [Required]
Each element is an object which has the following properties:
-
action
: String [Required]
Function name to be routed. -
method
: String [Default:'get'
]
Request method to be routed. -
path
: String [Default:''
]
Virtual path for theaction
. -
authorized
: Boolean [Default:true
]
Authorization is required or not for theaction
. -
validator
: Object [Default:undefined
]
Validator(s) for placeholders inpath
. -
middleware
: {Function|Function[]} [Default:undefined
]
Middleware(s) to be passed to Express.
preset
: Object [Default: undefined
]
The following properties will be applied to each element in routes
:
-
authorized
: Boolean [Default:true
]
Authorization is required or not. This can be overwritten byroutes[].authorized
. -
validator
: Object [Default:undefined
]
Validator(s) for placeholders in eachpath
. -
middleware
: {Function|Function[]} [Default:undefined
]
Middleware(s) to be passed to Express. -
template
: String [Default:undefined
]
One of templates to be used asroutes
.
Validator
If routes[].path
contains any placeholders to receive parameters from req.params
object, validator
will be helpful to validate parameters before calling routes[].action
.
The following example uses validator.js as validator
:
const validator = ; 'use strict'; moduleexports = __exprest: routes: action: 'view' path: ':id' validator: id: validatorisNumeric { // `req.params.id` must a number. res; }
You can also use a RegExp
object instead of a function as follow:
'use strict'; moduleexports = __exprest: routes: action: 'view' path: ':id' validator: id: /^[1-9][0-9]*/ { // `req.params.id` must a number. res; }
If you have a placeholder commonly used in a controller, you can use preset.validator
as follow:
'use strict'; const validator = ; moduleexports = __exprest: preset: validator: id: validatorisNumeric routes: action: 'view' path: ':id' action: 'update' path: ':id' method: 'put' action: 'remove' path: ':id' method: 'delete' { res; } { res; } { res; }};
Middleware
middleware
is typically used for APIs accept file uploading.
The following example uses Multer as middleware
:
'use strict'; const multer = upload = ; moduleexports = __exprest: routes: action: 'echo' method: 'post' middleware: upload { res; };
Another use case is authentication.
The following example uses Passport as middleware
:
'use strict'; const passport = ; moduleexports = __exprest: routes: action: 'login' middleware: passport { res; };
If you want to authenticate users for every action in a controller, you can use preset.middleware
as follow:
'use strict'; const passport = multer = upload = ; moduleexports = __exprest: preset: middleware: passport routes: action: 'login' action: 'echo' method: 'post' middleware: upload { res; } { res; };
Template
template
is an option to avoid defining the same routes
in multiple controllers.
Once you define routes which might be used often in several controllers, then feed them to route()
with opts.templates
, each controller can refer one of the templates through preset.template
property.
The following example shows how to use template:
// app.js 'use strict'; const express = exprest = app = templates = queue: action: 'push' path: ':elem' method: 'post' action: 'pop' method: 'delete' ; exprest
// controllers/seat.js 'use strict'; let waiting_list = ; moduleexports = __exprest: preset: template: 'queue' // No routes required here. { waiting_list; res; } { waiting_list; res; };
exprest4
provides the following template crud
by default which might be useful for CRUD-based REST APIs:
action: 'list' action: 'view' path: ':id' action: 'create' method: 'post' action: 'update' path: ':id' method: 'put' action: 'remove' path: ':id' method: 'delete'
If you define routes
along with preset.template
, each route in routes
will be appended to the template routes as a result.
In addition, routes
which has exactly the same method
and path
overwrites corresponding routes in the template.
License
The MIT License (MIT)
Copyright (c) 2016-2018 Hiro Fujita bow.fujita@gmail.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.