如何在nodejs中使用相同的文件执行export.module?

时间:2016-06-03 16:43:07

标签: javascript node.js

我有module.export.process我想执行此功能当我命令节点app.js它应该执行该功能,我试图再次导出它但是这不起作用,我是nodejs的新手任何帮助将不胜感激。

app.js

    // Include blinlib to access blink common utility modules
    var blinklib = require('../blinklib.js');
    var snmp = require("net-snmp");
    var logger = blinklib.logger('actors.snmp');
    var msg = require('./event.js');
    var config = require('./config.json');
    console.log('CONFIG', config);
    var session = snmp.createSession("127.0.0.1", "public");

    module.exports.process = function (msg, config, callback) {
        var informOid = msg.event.body.data[0].oid;
        var varbinds = msg.event.body.data;
        var options = {upTime: 1000};
        logger.info("Processing message: ", msg.event.message);
        varbinds.forEach(function (value) {
            value.type = snmp.ObjectType.OctetString;
        });
        try {
            session.inform(informOid, varbinds, options, function (error) {
                if (error)
                    console.error(error);
            });
        } catch (e) {
            logger.info(e);
        }
    }
module.export = process;

1 个答案:

答案 0 :(得分:3)

只需将流程功能导出到外部模块:

process.js

//Import dependencies    

function process(msg, config, callback){}

module.exports = process; 

app.js

var process = require('./process.js'); 
//declare msg, config, callback
process(msg, config, callback); 

或者,不要导出任何内容并以IIFE身份执行流程:

(function process(msg, config, callback){})();