本文介绍了如何从客户端javascript调用特定的node.js方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我在node.js文件中创建了许多方法。如何从客户端javascript调用特定方法。
In my application i have created many methods in node.js file.How can i call the particular method from client side javascript.
下面是我的node.js file
Below is my node.js file
exports.method1=function(){
}
exports.method2=function(){
}
exports.method3=function(){
}
推荐答案
您的客户应该发送消息,例如:
Your client should send a message, for example:
socket.emit("callMethod", {"methodName":"method3"});
在您的服务器中:
socket.on("callMethod", function(data) {
if(data["methodName"] == "method3") {
exports.method3();
}
});
您不直接调用方法,发送事件/消息。
You don't call methods directly, you send events/messages.
这篇关于如何从客户端javascript调用特定的node.js方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!