2016-01-13 190 views
0

我有一个节点服务器,它接收来自我的Chrome扩展的http POST消息。在我的节点服务器上收到http post请求后执行脚本?

的javascript:

function RecieveMessage(req, res) { 
    console.log("got the message" 
    } 

我想知道如何当通过RecieveMessage功能接收到的消息执行我的节点服务器上举行了另一JS文件?什么是最好的方法来做到这一点?

回答

1

除非我误解了,这应该这样做:

var otherFile = require('./otherfile.js'); 
... 
function RecieveMessage(req, res) { 
    console.log("got the message"); 
    otherFile(args); 
} 

而且你`otherfile.js'应该公开这样的主要功能:

module.exports = function (args) { 
    // Your code goes in here 
}; 
+0

谢谢,什么是我想要将RecieveMessage函数中的'res'输入到我的'otherfile.js'中?会像res.write这样的工作? –

+0

如果你的otherfile.js把所有代码包装在'module.exports = function(res){/ * your code * /};'中,那么上面的代码就可以工作。 你在谈论采用'res'并将它作为参数传递给函数 - 该函数可以是使用'module.exports'和'require'的另一个文件。 – nias

+0

你的其他代码用'res'做什么?这可能有助于了解这一点。 – nias

相关问题