0.1.2 • Published 7 years ago
shazam-middleware v0.1.2
shazam-middleware
What is shazam?
- Shazam it's a
express
middleware.
What he does?
- HTTP request logger like
morgan
(Method, Status Code, Browser, IP) - Notify on slack channel when errors occurs.
Requirements
Installation
npm install --save shazam-middleware
or
yarn add shazam-middleware
Slack
- Activate webhook on slack, to get your UrlHook :D
- https://{yourslackname}.slack.com/apps/A0F7XDUAZ-incoming-webhooks
- Doc: https://api.slack.com/incoming-webhooks
Paramters Configuration
slack: {
urlHook: '' // Url Hook of your slack,
channel: '' // Name of your channel (Without '#'),
iconUrl: '' // Customize the icon of bot (default: "http://dclegends.wiki/images/d/d9/Shazam_Billy_Batson_Portrait.png"),
botusername: '' // Customize the name of bot (default: Shazam)
},
api: {
name: '' // Name of your api,
version: '' // Version of your api
}
Code example to do the Magic! (Automatic)
const app = require('express')();
// Don't forget to configure the paramters
const shazam = require('shazam-middleware')({
slack: {
urlHook: process.env.urlHook,
channel: process.env.channel,
iconUrl: process.env.iconUrl,
botusername: process.env.botusername
},
api: {
name: 'My App',
version: 1
}
});
// Activate validation on process `uncaughtException` and `unhandledRejection`
shazam.handler;
// Now, we wanna log all requests
app.use(shazam.log);
// Look Here, one route with error
app.get("/error", (req, res, next) => {
batata
res.status(200).send({ retorno: 'ok' })
})
// Now, we will look for all exceptions from ours routes
app.use(shazam.exception);
// Start the Server
app.listen(4600, () => {
console.log('APP WORKS http://localhost:4600')
})
// Fine! It`s All!
If you return a object like this patern, and use next(), you can trigger the message on Slack Channel.
app.get("/error/next", (req, res, next) => {
next({
message: {
userMessage: 'Hey, user! Don´t Worry',
developerMessage: 'Hey Man! This is bad'
},
statusCode: 500
});
})
Example of error log
Example of log
Example of response
Examples on Slack
TODO
- Check Lazy Requests
- Save requests on database (mongo?)