Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node JS-从客户端javascript调用服务器上的函数_Javascript_Node.js - Fatal编程技术网

Node JS-从客户端javascript调用服务器上的函数

Node JS-从客户端javascript调用服务器上的函数,javascript,node.js,Javascript,Node.js,我已经写了一个server.js。代码如下:- var http = require('http'); var fs = require('fs'); var my_code = require('./my_code'); function send404Response (response) { response.writeHead(404, {"Context-Type" : "text\plain"}); response.write("Error 404: Page n

我已经写了一个server.js。代码如下:-

var http = require('http');
var fs = require('fs');
var my_code = require('./my_code');

function send404Response (response) {
    response.writeHead(404, {"Context-Type" : "text\plain"});
    response.write("Error 404: Page not found");
    response.end();
}

function onRequest(request, response) {
    if (request.method == 'GET' && request.url == '/') {
        response.writeHead(200, {"Context-Type" : "text\plain"});
        fs.createReadStream ('./index.html').pipe(response);
    } else {
        send404Response(response);
    }
}

http.createServer(onRequest).listen(8888);
console.log("Server is now running");
var https = require('https');
module.exports.func = myFunction;

function myFunction(myParam) {
    console.log (myParam);
}
在节点中编写的另一个服务器端js是my_code.js。代码如下:-

var http = require('http');
var fs = require('fs');
var my_code = require('./my_code');

function send404Response (response) {
    response.writeHead(404, {"Context-Type" : "text\plain"});
    response.write("Error 404: Page not found");
    response.end();
}

function onRequest(request, response) {
    if (request.method == 'GET' && request.url == '/') {
        response.writeHead(200, {"Context-Type" : "text\plain"});
        fs.createReadStream ('./index.html').pipe(response);
    } else {
        send404Response(response);
    }
}

http.createServer(onRequest).listen(8888);
console.log("Server is now running");
var https = require('https');
module.exports.func = myFunction;

function myFunction(myParam) {
    console.log (myParam);
}
myFunction(myParam)应该从客户端javascript调用,后者将传递myParam。但客户端javascript抛出错误,表示找不到myFunction

请在下面查找包含客户端javascript的HTML:-

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#button').click(function() {
        $.ajax('/', function(list) {
            myFunction($('#myField').text()); //How to do it
        });
    });
});
</script>
</head>
<form>
    <div id="contactno">myField:<input type="text" name="myField">  </div>
    <div id="button"><input type="button" value="Submit"></div>       
</form>
</html>

$(文档).ready(函数(){
$(“#按钮”)。单击(函数(){
$.ajax(“/”,函数(列表){
myFunction($('#myField').text();//如何操作
});
});
});
我的领域:
在点击上面的html页面之前,我正在运行node server.js
请让我知道从客户端调用myFunction的最佳方式。

提前感谢。

显然,正如您所了解的,您无法在客户端上调用此函数,因为它在客户端不可用。所以你的客户端必须调用服务器,对吗

因此,客户机代码必须将
$(“#myField”).text()传递给服务器,服务器必须有另一个端点才能接收该消息,调用
myFunction
,然后返回结果

你可以用很多不同的方法来做,这里有一个简单的例子

使用另一个端点展开服务器

function onRequest(request, response) {

  if (request.method == 'GET' && request.url == '/') {
    response.writeHead(200, {"Context-Type" : "text\plain"});
    fs.createReadStream ('./index.html').pipe(response);
  } else if (request.method === 'POST' && request.url == '/do-work') {

    // asuming sync as in your example
    let result = my_code.func(request.body); 
    response.writeHead(200, {"Context-Type" : "application/json"});
    response.write(JSON.stringify({

      result: result
    });
    response.end();
  } else {
    send404Response(response);
  }
}
(为了让我的回答集中在你的问题上,我在这里不涉及一些不太好的实践和代码。)

您还必须扩展客户端代码

$('#button').click(function() {
    $.ajax('/', function(list) {
        $.ajax({
            type: 'POST',
            url: '/do-work',
            data: $('#myField').text()
            success: function(result) {
              console.log('Response:', result);
            }
        }
    });
});

对因为它是服务器端。它不在客户机中。您希望能够从客户端javascript调用PHP函数吗?HTTP是一种只发送文本的超级简单协议。因此,它不会神奇地使服务器端功能出现在客户机中。是的,您是正确的。服务器端功能在客户端将不可用。所以,我不应该直接尝试从客户端访问myFunction。这就是为什么我要发表评论——如何做
但是从客户端调用nodeJs中的服务器端函数的最佳方式是什么呢?由于降价幅度太大,我在格式化这个时遇到了很多问题!是的,我知道,在电话上发帖,和格式化这件事斗争!嘿,兹拉特科,谢谢你的回复。但当我尝试进行上述ajax调用时,也在server.js上,它被检索为type=GET和url=“/”。