Nautical Pace Marker

    express-notify-io

    0.0.3 • Public • Published

    Express Notify-IO

    Easy to setup middleware for notify-io.

    Getting Started:

    Initial Setup

    const { SchemaBuilder, Notify } = require('notify-io')
     
    const schema = new SchemaBuilder()
     
    schema.create('unknown', {
        en: () =>`something went wrong`,
        zh: () =>`服务器有误`,
    })
     
    const instance = () => new Notify(schema)
     
    const notifyStatusTo = require('express-notify-io')(instance)
     
    app.use(notifyStatusTo('error'))
    app.use(notifyStatusTo('validation'))
     

    Usage in routes

        app.use((req, res, next) => {
            const { error, validation } = req.tools
            error(name, data, key)
            if(false) next(error())
     
            // or directly call error in your next function
     
            if(false) next(error(name, data, key)())
     
            // in no errors then continue to process code ...
        })

    Usage in validation middleware

        app.use((req, res, next) => {
            const { validation } = req.tools
            const { validationErrors } = req.body
     
            if(validationErrors.length > 0){
     
                for(let err of validationErrors){
                    const = { name, data, key } = err
                    validation(name, data, key)
                }
     
                next(validation())
            }else{
                next()
            }
        })
     

    Handle in errorHandler route

        app.use((err, req, res, next) => {
            
            let _error = null
     
            if(err instanceof Notify) {
                _error = err
            }else {
                const { error } = req.tools
                _error = error('unknown')
            }
     
            const msg = _error.langTo('zh').render()
            res.status(500).json(msg)
        })

    output:

    {
        "lang": "zh",
        "state": "error",
        "messages": [
            {
                "message": "服务器有误"
            }
        ]
    }

    Install

    npm i express-notify-io

    DownloadsWeekly Downloads

    6

    Version

    0.0.3

    License

    MIT

    Unpacked Size

    3.73 kB

    Total Files

    4

    Last publish

    Collaborators

    • chen7david