express-route-builder
Advanced tools
Comparing version 0.0.9 to 0.0.10
61
index.js
@@ -56,10 +56,19 @@ 'use strict'; | ||
*/ | ||
addStatic (dir, mountPath = null) { | ||
addStatic (dir, prefix = null, _middleware = [], options = {}) { | ||
if (mountPath) { | ||
this.app.use(this.express.static(mountPath, dir)); | ||
} else { | ||
this.app.use(this.express.static(dir)); | ||
const middleware = this.prepareMiddlewareArray(_middleware); | ||
const args = []; | ||
const { mResult, mIndex, mType } = this.checkMiddlewareMethods(middleware); | ||
if (!mResult) { | ||
throw new Error(`Middleware for static directory "${dir}" at index ${mIndex} should be a function and not "${mType}"!`); | ||
} | ||
// Build the arguments to pass to app.use(). | ||
if (prefix) { args.push(prefix); } | ||
if (middleware && middleware.length) { args.push(middleware); } | ||
args.push(this.express.static(dir, options)); | ||
this.app.use.apply(null, args); | ||
} | ||
@@ -72,3 +81,3 @@ | ||
let middleware = _middleware || []; | ||
const middleware = this.prepareMiddlewareArray(_middleware); | ||
let fullFilename; | ||
@@ -92,11 +101,6 @@ let module; | ||
// Check each of the middleware methods. | ||
middleware = (Array.isArray(middleware) ? middleware : [middleware]); | ||
for (let m = 0, mlen = middleware.length; m < mlen; m++) { | ||
const middlewareFn = middleware[m]; | ||
const { mResult, mIndex, mType } = this.checkMiddlewareMethods(middleware); | ||
if (typeof middlewareFn !== 'function') { | ||
const type = typeof middlewareFn; | ||
throw new Error(`Middleware for path "${path}" at index ${m} should be a function and not "${type}"!`); | ||
} | ||
if (!mResult) { | ||
throw new Error(`Middleware for path "${path}" at index ${mIndex} should be a function and not "${mType}"!`); | ||
} | ||
@@ -135,2 +139,31 @@ | ||
/* | ||
* Ensures the middleware is an array. | ||
*/ | ||
prepareMiddlewareArray (middlewareList) { | ||
if (!middlewareList) { return []; } | ||
return (Array.isArray(middlewareList) ? middlewareList : [middlewareList]); | ||
} | ||
/* | ||
* Ensure all middlewares are valid. | ||
*/ | ||
checkMiddlewareMethods (middlewareList) { | ||
// Check each function in turn. | ||
for (let index = 0, ilen = middlewareList.length; index < ilen; index++) { | ||
const middlewareFn = middlewareList[index]; | ||
if (typeof middlewareFn !== 'function') { | ||
const type = typeof middlewareFn; | ||
return { mResult: false, index, type }; | ||
} | ||
} | ||
// All OK! | ||
return { mResult: true }; | ||
} | ||
}; |
@@ -10,3 +10,3 @@ { | ||
"name": "express-route-builder", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Quickly compile Express.js routes with minimal code.", | ||
@@ -13,0 +13,0 @@ "keywords": [ |
@@ -7,2 +7,3 @@ # Express-Route-Builder | ||
## Quick Use | ||
The following example will help you get up and running quickly but you can also take a look at the API reference further down which contains more options. | ||
@@ -32,4 +33,12 @@ #### index.js | ||
// You can specify the name of the controller files for each path. | ||
builder.addRoute('/comments', 'comments'); | ||
builder.addRoute('/users', 'users'); | ||
// Or you can explicitly define functions for each route and method. | ||
builder.addRoute('/other/data', { | ||
get: (req, res, next) => { ... }, | ||
post: (req, res, next) => { ... }, | ||
... | ||
}) | ||
}; | ||
@@ -71,6 +80,6 @@ ``` | ||
#### .addRoute(path, filename, middleware = []); | ||
Adds a route to your Express app based on the path given; you can specify any path that Express accepts. The filename should be the name of the module which will handle this route, relative to the base path given in the constructor. Also, you can optionally specify an array of middleware functions to use before the route is processed. | ||
#### .addRoute(path, input, middleware = []); | ||
Adds a route to your Express app based on the path given; you can specify any path that Express accepts. The `input` parameter should be either a filename of the module which will handle this route (relative to the base directory given in the constructor) or an object containing functions with keys based on the HTTP methods supported by express. Also, you can optionally specify an array of middleware functions to use before the route is processed. | ||
#### .addStatic(dir, prefix = null); | ||
Adds a directory to your express app where static files will be served up from. The directory will be relative to the current working directory of your app so it's a good idea to specify an absolute path. The prefix parameter is optional and will be prepended to the file path in the URL, just like with express.static(). | ||
#### .addStatic(dir, prefix = null, middleware = [], options = {}); | ||
Adds a directory to your express app where static files will be served up from. The directory will be relative to the current working directory of your app so it's a good idea to specify an absolute path. The prefix parameter is optional and will be prepended to the file path in the URL, just like with express.static(). Also, you can optionally specify an array of middleware functions to use before any files in the static directory are served up. The `options` parameter is the same as `express.static()`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9315
136
83