如何将控制器变为帆的后台进程?

时间:2015-06-05 01:42:15

标签: sails.js

dI有一个名为“test”的控制器,它位于下面,是“数据”模型的控制器。它连接到远程流媒体服务器。

我希望立即连续收集流数据并发布到“数据”端点。我被告知引导或使它成为我需要的钩子,但我不知道我会怎么做。

谢谢

/**
* dataController
*
* @description :: Server-side logic for managing data
* @help        :: See http://links.sailsjs.org/docs/controllers
*/

module.exports = {

test: function(req, res) {
    var https = require('https');

    var access_token='***********';
    var account_id = 'hippo';

    var options = {
        host: 'streaming.website.com',
        path: '/data?accountId=' + account_id,
        method: 'GET',
        headers: {'Authorization': 'Bearer ' + access_token}
    };

    https.request(options, function(response) {
        var responseData ='';
        //response.setEncoding('utf8');

        response.on('data', function (chunk){
            responseData+= chunk.toString();
            console.log(responseData);
        });

        response.once('error', function(err) {
            // Some error handling here e.g.:
            res.serverError(err);
        });

        response.on('end', function() {
            try {
                // response available as 'responseData' in 'data'
                res.locals.requestData = JSON.parse(responseData);
            } catch (e) {
                sails.log.warn('Could not parse response from options.hostname: ' + e);
            }

            res.view('homepage');

        });
    }).end();
}

0 个答案:

没有答案