express notification system
you can send notification with diffrent channels
first of all you must install the package
run this command first :
npm i express-notification
- you can see ./example directory for a real example
after you install the package you can require express notification and then you must register a middleware for the package
const express = require('express')
const app = express()
const notification = require('express-notification');
// define config of notification package
const notificationConfig = {
config : {}
}
// register notification system
app.use(notification.register(notificationConfig))
list of default channels:
- mail channel
you have one channel by defualt named 'mail channel', you must set config to use it
// define config of notification package
const notificationConfig = {
config : {
mail : {
// you can config your smtp server
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: '',
pass: '',
}
}
}
}
// register notification system
app.use(notification.register(notificationConfig))
now you must define a notification to send.
maybe in ./notifications/exampleNotification.js
class ExampleNotification {
/**
* return list of channels that you want to send notification to
* @returns {string[]}
*/
via() {
return ['mail'];
}
/**
*
* @param {{ email : string }} notifiable
* @returns {{subject: string, from: string, html: string, to}}
*/
toMail(notifiable) {
return {
from : '[email protected]',
to : notifiable.email,
subject: 'hello world',
html : `
<h2>Hello World</h2>
`
}
}
}
module.exports = () => new ExampleNotification()
now you can easily send notifition in Route Handler
const exampleNotification = require('./notifications/exampleNotification');
app.get('/', (req, res) => {
// send notificaiton
res.notify(exampleNotification()).to({ email : '[email protected]' })
res.send('Hello World!')
});
you can define custom channels for sending your notifications
maybe in ./notifications/channels/smsChannel.js
class SmsChannel {
// init MailChannel
constructor (config) {
// check mail config is exsits in config
if(! ('sms' in config)) {
throw new Error('sms config is mixing')
}
}
/**
*
* @param notifiable
* @param {Notification} notification
*/
async send(notifiable , notification ) {
// this message comes from Notification classes
let message = notification.toSms(notifiable);
let { phoneNumber } = notifiable;
// can require an api to send sms here
}
}
module.exports = SmsChannel;
when you registered express notification middleware, you can set custom channels in notification config
// we could define the channel in everywhere we want
const SmsChannel = require('./notifications/channels/smsChannel.js')
// define config of notification package
const notificationConfig = {
config : {
mail : {
// you can config your smtp server
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: '',
pass: '',
}
},
sms : {
// sms channel config
}
},
channels : {
'sms' : SmsChannel
}
}
// register notification system
app.use(notification.register(notificationConfig))
you can use the channel in every notification class you want
class ExampleNotification {
/**
* return list of channels that you want to send notification to
* @returns {string[]}
*/
via() {
return ['mail' , 'sms'];
}
/**
*
* @param {{ email : string }} notifiable
* @returns {{subject: string, from: string, html: string, to}}
*/
toMail(notifiable) {
return {
from : '[email protected]',
to : notifiable.email,
subject: 'hello world',
html : `
<h2>Hello World</h2>
`
}
}
toSms(notifiable) {
return {
message : 'hello roocket'
}
}
}
module.exports = () => new ExampleNotification()