如何从一个节点到另一节点文件获取数据?

时间:2020-05-09 09:21:48

标签: node.js firebase google-cloud-functions

我有2个节点文件,一个节点我称它为test.js节点,该节点位于根文件夹中,现在我的另一个节点js文件位于functions/index.js中,我想在索引中运行我的测试节点.js文件,在这里我已经输入了该文件的代码,任何人都可以帮助我如何获取其数据吗?

test.js(位于根文件夹中)

const GeotabApi = require('mg-api-js');
const authentication = {
    credentials: {
        database: '***',
        userName: '***',
        password: '***'
    }
}
const api = new GeotabApi(authentication);

api.call('Get', { typeName: 'Group', resultsLimit: 100 })
    .then(result => {
        return result;
    })
    .catch(error => {
        return error;
    });

我想将那个test.js文件调用到我的index.js中,我已经放置了index.js文件的代码,但是我没有得到它的输出

functions / index.js

var childProcess = require('child_process');

function runScript(scriptPath, callback) {

    // keep track of whether callback has been invoked to prevent multiple invocations
    var invoked = false;

    var process = childProcess.fork(scriptPath);

    // listen for errors as they may prevent the exit event from firing
    process.on('error', function (err) {
        if (invoked) return;
        invoked = true;
        callback(err);
    });

    // execute the callback once the process has finished running
    process.on('exit', function (code) {
        if (invoked) return;
        invoked = true;
        var err = code === 0 ? null : new Error('exit code ' + code);
        callback(err);
    });

}

exports.test_groups_list = functions.https.onRequest(async (req, res) => {
    runScript('../test.js', function (err) {
        if (err) {
            res.set({ 'Access-Control-Allow-Origin': '*' }).send({ "status": 0,"msg":err });
        } else {
            res.set({ 'Access-Control-Allow-Origin': '*' }).send({ "status": 1 });
        }
    });
});

0 个答案:

没有答案