Pragonauts Express Tools
Usefull tools for:
- starting express app in one line
- handling errors within Express.js with
app.use(errorMiddleware())
- throwing nice JSON errors with
res.throw(401);
- measuring request duration and logging with
app.use(requestLogger())
- cache some static data in templates using
hbsStaticHelpers
- having nice
new AppError('Missing attribute', 400, 4596)
- forcing slashes in url with
configurator
- redirecting to https with
configurator
- enabling trust proxy with
configurator
- enabling compression with
configurator
API
Classes
Functions
- createWebserver(app, [port], [log], [proc]) ⇒
Object
Simpifies starting an Express.js server
- configurator(app, httpConfig, [compressionMiddleware])
- errorMiddleware([options], log) ⇒
errorMiddleware~error
Return error processing middleware
- throwMiddleware(logger, muteLogs) ⇒
throwMiddleware~throwMid
Creates middleware, which allows simple logging and error responding in API
- requestLogger(callback, [muteLogs]) ⇒
requestLogger~logger
Creates express middleware for logging duration of requests
- hbsStaticHelpers(expressHbs, helpers, [data], [hbs]) ⇒
ExpressHandlebars
Attaches compiler helpers which makes some static data compiled once
{AppError}
new {AppError}()
Class with prepared status and code for {throwMiddleware} and for {errorMiddleware}
AppError
new AppError(error, status, code)
Creates an instance of AppError.
Param | Type |
---|---|
error | string |
status | number |
code | number |
Object
createWebserver(app, [port], [log], [proc]) ⇒ Simpifies starting an Express.js server
Kind: global function
Returns: Object
- description
Param | Type | Default | Description |
---|---|---|---|
app | any |
server listener | |
[port] | number | string |
3000 |
listening port, default 3000 |
[log] | console |
||
[proc] | process |
for testing purposes |
Example
const express = 'express';const createWebserver = ; const app = ;start;
configurator(app, httpConfig, [compressionMiddleware])
Kind: global function
Param | Type | Description |
---|---|---|
app | Express |
application |
httpConfig | object |
the configuration |
[httpConfig.zlib] | object | boolean |
compression configuration |
[httpConfig.forceSlashes] | boolean | null |
forcing slashes |
[httpConfig.trustProxy] | boolean |
enables proxytrust |
[httpConfig.isUsingSsl] | boolean |
forces https |
[httpConfig.excludeRedirectPaths] | array |
forces https |
[compressionMiddleware] | func |
Example
// theese are default valuesconst configurator = ; ;
errorMiddleware~error
errorMiddleware([options], log) ⇒ Return error processing middleware
Kind: global function
Returns: errorMiddleware~error
- - the middleware
Param | Type | Description |
---|---|---|
[options] | Object |
|
log | console |
the logger |
Example
const errorMiddleware = ; app;
throwMiddleware~throwMid
throwMiddleware(logger, muteLogs) ⇒ Creates middleware, which allows simple logging and error responding in API
Kind: global function
Returns: throwMiddleware~throwMid
- - the middleware
Param | Type | Default | Description |
---|---|---|---|
logger | console |
where to log | |
muteLogs | boolean |
false |
use true for tests |
Example
const express = ; const app = ; app; app;
requestLogger~logger
requestLogger(callback, [muteLogs]) ⇒ Creates express middleware for logging duration of requests
Kind: global function
Returns: requestLogger~logger
- - returns the middleware
Param | Type | Default | Description |
---|---|---|---|
callback | func |
called, when request is finnished | |
[muteLogs] | boolean |
false |
mutes logs for testing |
Example
app;
ExpressHandlebars
hbsStaticHelpers(expressHbs, helpers, [data], [hbs]) ⇒ Attaches compiler helpers which makes some static data compiled once
Kind: global function
Param | Type | Description |
---|---|---|
expressHbs | ExpressHandlebars |
Express Handlebars |
helpers | Object |
map of helpers |
[data] | Object |
static data |
[hbs] | Handlebars |
handlebars library |
Example
const hbsStaticHelpers = ; // setup templatingconst hbs = expressHandlebars; // setup template cachingif !configdebugEnabled app; // setup compile-time helpers {{$xkf kdkd}}; // attach template engineapp;app;app; // will be staticly executed in compilation time$staticHelper 'param'// will be staticly loaded from data$dataParam