2017-06-29 1 views
0

私は以下のように羽サービスAPIを定義します。feathersjs websocket apiでプレーンテキストに返信するには?

class Monitor { 

    find(_) { 
    const metrics = prom.register.metrics(); 
    log.info(metrics); 
    return new Promise((resolve) => { 
     resolve({text: metrics}); 
    }); 
    } 
} 

function restFormatter(req, res) { 
    res.format({ 
    'text/plain': function() { 
     log('xxxx:', res); 
     res.end(`The Message is: "${res.data}"`); 
    } 
    }); 
} 

module.exports = function() { 
    const app = this; 

    // Initialize our service with any options it requires 
    const service = new Monitor(); 
    app.configure(rest(restFormatter)).use('/metrics', service); 

    // Get our initialize service to that we can bind hooks 
    const monitorService = app.service('/metrics'); 

    // Set up our before hooks 
    monitorService.before(hooks.before); 

    // Set up our after hooks 
    monitorService.after(hooks.after); 
    return service; 
}; 

module.exports.Monitor = Monitor; 

ブラウザからこのAPIを呼び出すとき、私は応答の下に取得する:

"# HELP nodejs_gc_runs_total Count of total garbage collections.\n# TYPE nodejs_gc_runs_total counter\n\n# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds.\n# TYPE nodejs_gc_pause_seconds_total counter\n\n# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC.\n# TYPE nodejs_gc_reclaimed_bytes_total counter\n" 

出力の上からあなたはfeathersjsが戻らないことがわかりますプレーンテキスト形式のデータ。私の応答テキストを文字列に変換します。以下は、ブラウザに表示expressサービスからの出力は次のようになります。

# HELP nodejs_gc_runs_total Count of total garbage collections. 
# TYPE nodejs_gc_runs_total counter 

# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds. 
# TYPE nodejs_gc_pause_seconds_total counter 

# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC. 
# TYPE nodejs_gc_reclaimed_bytes_total counter 

# HELP newConnection The number of requests served 
# TYPE newConnection counter 

この出力は、私が本当に欲しいものです。どうすれば出力の上にfeathersjsサービスの復帰をさせることができますか?以下は

は私のfeathersjs構成部分である:

app 
    .use(compress()) 
    .options('*', cors()) 
    .use(cors()) 
    .use('/', serveStatic(app.get('public'))) 
    .use(bodyParser.json()) 
    .use(bodyParser.urlencoded({extended: true})) 
    .configure(hooks()) 
    .configure(rest()) 
    .configure(
    swagger({ 
     docsPath: '/docs', 
     uiIndex: path.join(__dirname, '../public/docs.html'), 
     info: { 
     title: process.env.npm_package_fullName, 
     description: process.env.npm_package_description 
     } 
    }) 
) 
    .configure(
    primus(
     { 
     transformer: 'websockets', 
     timeout: false 
     }, 
     (primus) => { 
     primus.library(); 
     primus.save(path.join(__dirname, '../public/dist/primus.js')); 
     } 
    ) 
) 
    .configure(services) 
    .configure(middleware); 

答えて

0

あなたはまだ古い出力を得る理由である二回feathers-restを設定しています。あなたのサービスファイルからapp.configure(rest(restFormatter))を削除し、その後、いずれかのすべてのサービスに適用するか、単にそのサービスのための書式設定を行いcustom middleware for the serviceを登録するフォーマッタを使用するには、メインファイルに.configure(rest(restFormatter)).configure(rest())を変更します。

app.use('/metrics', service, function(req, res) { 
    res.format({ 
    'text/plain': function() { 
     log('xxxx:', res); 
     res.end(`The Message is: "${res.data}"`); 
    } 
    }); 
}); 
関連する問題