2017-06-29 47 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配置部分:

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)),然後無論是在主文件更改.configure(rest()).configure(rest(restFormatter))使用格式適用於所有的服務或註冊custom middleware for the service,做的格式只針對該服務:

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