0.0.15 • Published 6 years ago
jlocke-express-middleware v0.0.15
jlocke-express-middleware
Express middleware to store requests metadata to an Elastic instance. We use it in JLocke project and other internal ones.
Install
npm i --save jlocke-express-middleware
Use
Please visit the tests to see a full example. NOTES:
- The "bodyParser" should be attached before the middleware to capture the body of the POST requests.
- Routes need to be defined after connecting this middleware to Express.
async jLocke.init(uri, opts)
-> Promise
The middleware setup expects:
uri
(string) - The database uri to connect to.opts
(object) - DB optional values:trace
(boolean) - To enable Elastic tracing. (default: false)indexRequests
(string) - Name of the Elastic index to store the requests info. (default: "api-requests-MM-DD-YYY")typeRequests
(string) - Elastic type of the element to store the requests info. (default: "requests")indexErrors
(string) - Name of the Elastic index to store the errors info. (default: "api-errors-MM-DD-YYY")typeErrors
(string) - Elastic type of the element to store the errors info. (default: "errors")
async jLocke.error(message, error, opts)
-> Promise
To track the errors, ie: uncaughException, unhandledPromise. The middleware setup expects:
message
(string) - Custom error message.error
(object) - JavaScriptError
object.opts
(object) - Optional values:userId
(string) - User identifier to trace the error.
jLocke.express(opts)
-> function
To track the Express requests info. The middleware expects to receive the next parameters. Please visit the tests for more details.
opts
(object) - Optional values:path
(string) - To store only the requests through this subpath. (default: null)hide
(object) - To avoid to store sensitive data in the DB for a POST route. (default: {})path
(string) - The substring of the path to exlude stuff (ie: "login").field
(string) - Name of the object field to exclude (ie: "password").
Adding more parameters
Apart from the ones included by Express you can attach to the req
the user ID to allow tracing:
userId
(string) - User identifier.
Example here.
Developer guide
Please check this link before a contribution.