ack-node
Common server-side functionality, wrapped in one package, the Acker way
Table of Contents
Installation
$ npm install ack-node --save
Usage
var ack =
Modules
ack.promise
See ack-p for full details
executed already running promise
ack
ack.Promise
Traditional resolve/reject promise
ack
ack.ip
Internet Protocol address functionality
/** matches 192.168 and other internal network ips */ack /** matches host machine ips */ack
ack.path - system directory functionality
See ack-path for full details
//created directory if not existantack //string manipulationackpath// = __dirname/a/b/c.js //string manipulationackpath// = /test/file //string manipulationackpath// = /test/file //string manipulationackpath// = /test/ //string manipulationackpath// = /test/ //path delete promiseack ack//NONASYNC
ack.file
System file functionality
See ack-path for full details
ackack//Ex: application/javascriptackackack //string manipulationsackpathackpath//creates new instance, leaving original aloneackpath//Manipulates path by removing one file extension
ack.jwt
json web tokens. See jsonwebtoken for full details
var payload = some:'data'hello:'world'var signed = ack ack
ack.req
Outbound http/https requests based on functionality provived by request
Send Example
ack
Send Example Without Promise Spread
ack
Post Example
ack
Put Example
ack
Delete Example
ack
ack.reqres
Request response handler
sendJson(variable, pretty)
app
send file
app
ack.router
Access to Middleware
const ackRouters =
Mix with Connect or Express
var app = //common request routing app //Ignore fav.ico, timeout in 3000ms, and all requests will be gzipped if applicableapp
Router Table of Contents
- .cacheFor(seconds)
- .notFound( optional_message )
- .timeout(ms, options)
- .compress(options)
- .cors(options)
- .relocate(url)
- .ignoreFavors()
- .closeDevErrors()
- .htmlCloseError(options)
- .jsonCloseError(options)
- .closeProductionErrors()
- .consoleNonProductionErrors(options)
- .urlVarAsAuthHeader(varName)
- .cookieAsAuthHeader(varName)
- .jwt(secret,options)
- .logging(format,options)
- .logToArray(options)
- .errorsToArray(options)
- .uploadByName(name, options)
- .methodNotAllowed(message)
- .throw(ErrorOrMessage)
- .parseBody(options)
- .parseMultipartFields()
- .uploadOneByName(name, options)
- .uploadOneByNameToPath(name, path, options)
- .uploadArrayByName(name, options)
- .localNetworkOnly(message)
- .noRobots()
.cacheFor(seconds)
- return middleware that sets cache-control header for every request
.notFound( optional_message )
- optional_message: default='Not Found - ${path}'
.timeout(ms, options)
- returns middleware that forces requests to timeout. Uses npm connect-timeout
.compress(options)
- returns middleware that GZIP requests. See npm compression
.cors(options)
- returns middleware for cross orgin services
- options {origin:'url-string'}. No options means allow all. See package cors
.relocate(url)
- return middleware that pushes requests to a new url
.ignoreFavors()
- returns middleware that 404s requests matching typical fav.ico files
.closeDevErrors()
- returns middleware that closes errors with crucial details needed during development
.htmlCloseError(options)
- Returns universal error handler middleware
- options {debug:true/false, debugLocalNetwork:true}
.jsonCloseError(options)
- returns middleware that handles errors with JSON style details
- options {debug:true/false, debugLocalNetwork:true}
.closeProductionErrors()
- returns middleware that conditions errors returned to provide useful responses without exact detail specifics on excepetions thrown
.consoleNonProductionErrors(options)
- returns middleware that conditions errors returned to provide useful responses with exact detail specifics on excepetions thrown
.urlVarAsAuthHeader(varName)
- returns middleware that upgrades a url variable into an Authorization header
.cookieAsAuthHeader(varName)
- returns middleware that upgrades a cookie variable into an Authorization header
.jwt(secret,options)
-
returns middleware that handles the processing of JWT
-
options {...}
- requestKeyName: 'auth'//where parsed data will live (aka as requestProperty)
.logging(format,options)
returns middleware that makes server logging colorful and useful. Request-end result logging. See npm morgan.
- format
- default dev env format: 'dev' aka ':method :url :status :res[content-length] - :response-time ms'
- default pro env format: ':http-version/:method :url-short :colored-status :res[content-length] - :response-time ms :remote-addr :remote-user'
- "url-short" is a custom made morgan.token()
- "colored-status" is a custom made morgan.token()
.logToArray(options)
returns middleware the records requests to an array of specified maxLength. Uses .logging(format,options)
- options
- array:[]
- maxLength:25
.errorsToArray(options)
returns middleware the records errors to an array of specified maxLength
- options
- array:[]
- maxLength:25
.uploadByName(name, options)
- returns middleware that uploads files. Creates req.files array
- **options - see function paramUploadOptions
.methodNotAllowed(message)
- returns middleware that throws 405 errors on request
.throw(ErrorOrMessage)
- returns middleware that throws 400 errors on request
.parseBody(options)
- returns middleware that parses request bodies into request.body object
- options
- limit:102400
- see bodyParser for more
.parseMultipartFields()
- returns middleware that parse multi-part requests. Creates request.body which contains all form post fields
NOTE: Cannot be used with any other multipart reader/middleware. Only one middleware can read a stream
.uploadOneByName(name, options)
- returns middleware that uploads only one file. Creates req[name] file
- options - see function paramUploadOptions
- Cannot be used with any other multipart reader/middleware. Only one middleware can read a stream
- Any BODY/POST variables will be parsed and made available as req.body
.uploadOneByNameToPath(name, path, options)
- for more information see uploadOneByName
- name - input file field name expected to receive file on
- path - exact file path or if folder path, then file upload name will be used
- options - see function paramUploadOptions
NOTES:
- Cannot be used with any other multipart reader/middleware. Only one middleware can read a stream
- Any BODY/POST variables will be parsed and made available as req.body
.uploadArrayByName(name, options)
- returns middleware that uploads an array of files. Creates req[name] array
- options - see function paramUploadOptions
- Cannot be used with any other multipart reader/middleware. Only one middleware can read a stream
- Any BODY/POST variables will be parsed and made available as req.body
.localNetworkOnly(message)
- returns middleware that only allows local network requests
.noRobots()
- returns middleware that responds with a text/plain message of "User-agent: *\rDisallow: /"
ack-x
Additional lower level functionality is provided by ack-x
Date Example
The above and many more examples of modules provided by ack-x can be seen here