hapijs / good

hapi process monitoring
Other
525 stars 160 forks source link

Add Custom Transform Stream Option #486

Closed arb closed 5 years ago

arb commented 8 years ago

Add an example showing how you can add data using a custom transform stream. You could also leverage the good option passed in from a route handler or reply.

bableharry commented 5 years ago

I'm starting with things like this:

'use strict';

const Stream = require('stream');

const Hapi = require('hapi');
const Good = require('good');

const server = Hapi.Server({
    host: 'localhost',
    port: 3000
});

class SayHelloTransform extends Stream.Transform {
    constructor(options) {
        super({objectMode: true});
    }

    _transform(data, enc, next) {
        // Adds date to data
        const date = new Date(Date.now()).toLocaleString("en-US");
        next(null, `${data.tags}: ${date}: ${data.data}\n`);
    }
}

const init = async () => {

    await server.register({
        plugin: Good,
        options: {
            ops: {
                interval: 1000
            },
            reporters: {
                MyReporter: [new SayHelloTransform(), 'stdout']
            }
        }
    });

    await server.start();

    server.log('info', 'Server is running...')
};

init();
lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.