Giter VIP home page Giter VIP logo

hapijsdemo's People

Contributors

monvhh avatar

Watchers

 avatar  avatar

hapijsdemo's Issues

日志记录实战

如果打算用官方demo推荐的方式就不必看了。
由于公司有自己记日志的方式,所以必须自己实现。

首先,hapi.js提供一个EventEmitter的log事件。

在需要记日志的地方,调用

server.log
request.log
eg.

const handler = function (request, h) {

    request.log(['test', 'error'], 'Test event');
    return null;
};

然后在server上注册事件监听

server.events.on({ name: 'request', channels: 'app' }, (request, event, tags) => {

    if (tags.error) {
        console.log(event);
    }
});

server.events.on({ name: 'request', channels: 'internal' }, (request, event, tags) => {

    console.log(event);
});

channel:app/internal
internel应该是服务内部的日志;
request.log记录的日志目前使用下来都是app。

详情查看链接

通用记日志

在Request lifecycle中绑定事件。
比如,我主要是记录http请求到达和http返回响应

server.ext({
            type: 'onPreHandler',
            method: function (request, h) {
                request.log(['begin', 'api']);
                return h.continue;
            },
            options: {
                sandbox: 'plugin'
            }
        })

server.ext({
            type: 'onPreResponse',
            method: function (request, h) {
                    let data = request.response.source;
                    request.log(['end', 'api'], data);
            },
            options: {
                sandbox: 'plugin'
            }
        })


server.events.on({
        name: 'request',
        channels: 'app'
    }, (request, event, tags) => {
        if (tags.error) {
            callback();//调公司通用的记日志方法
            return;
        }
        if (tags.begin) {
            callback();//调公司通用的记日志方法
        } else if (tags.end) {
            callback();//调公司通用的记日志方法
        }
    });

Response统一封装

提供给客户端的响应,有一个固定的结构,懒得每次个接口,调reponse()的时候还要先调一下包装方法,重复劳动。需要抽离出来

万能的lifecycle

server.ext({
            type: 'onPreResponse',
            method: function (request, h) {
                    let data = request.response.source;

                    return h.response({
                        errCode: 0,
                        errMsg: '',
                        data: data
                    }).code(200);
            },
            options: {
                sandbox: 'plugin'
            }
        })

我写在plugin里了,因为会影响hapi-swagger.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.