Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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
Javascript 为什么不是';此服务器/客户端连接是否正常工作?_Javascript_Node.js - Fatal编程技术网

Javascript 为什么不是';此服务器/客户端连接是否正常工作?

Javascript 为什么不是';此服务器/客户端连接是否正常工作?,javascript,node.js,Javascript,Node.js,我正在使用node.js设置我的第一台服务器,但我不知道如何连接客户端和该服务器。我不想使用jquery,我能找到的所有关于jquery的问题都涉及到jquery或者关于不同的语言。有人知道怎么做吗 编辑:我在服务器和客户端之间有一个连接,但是响应中没有任何内容。我的服务器的代码是,我的客户端的代码是(在文件夹“Multiplayer”中)。您可以使用vanilla JavaScript,使用Fetch API来实现这一点 假设该节点将为您提供一些URL,您可以通过获取它们来获取、发布等 更多关

我正在使用node.js设置我的第一台服务器,但我不知道如何连接客户端和该服务器。我不想使用jquery,我能找到的所有关于jquery的问题都涉及到jquery或者关于不同的语言。有人知道怎么做吗


编辑:我在服务器和客户端之间有一个连接,但是响应中没有任何内容。我的服务器的代码是,我的客户端的代码是(在文件夹“Multiplayer”中)。

您可以使用vanilla JavaScript,使用Fetch API来实现这一点

假设该节点将为您提供一些URL,您可以通过获取它们来获取、发布等

更多关于如何工作的信息,请参见:

客户端和节点服务器之间的TCP连接将是选项。下面是一个示例代码片段:

var ser = require('ser');

var clientSer = new net.Socket();
clientSer.connect(1220, '127.0.0.1', function() {
    console.log('Connected');
    client.write('Hello, Connection Established!');
});

clientSer.on('data', function(data) {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
});

clientSer.on('close', function() {
    console.log('Connection closed');
});

节点教程:

执行类似操作,在端口8080上设置Node.js HTTP服务器侦听

客户端将使用AJAX发送GET请求


index.html

<html>
  <head>
    <script>
      var xhttp = new XMLHttpRequest();
      // Create a function callback, called every time readyState changes
      xhttp.onreadystatechange = function()
      {
        // When the response has been received with status 200
        // Update the element div#response-holder
        if (this.readyState == 4 && this.status == 200)
        {
          var txtDisplay = elem document.getElementById("response-holder")
          txtDisplay.innerHTML = this.responseText;
        }
      };

      // Send a GET request to /api, asynchronously
      xhttp.open("GET", "/api", true);
      xhttp.send();
    <script>
  </head>
  <body>
    <div id="response-holder"></div>
  </body>
</html>"
以下是关于AJAX的W3Schools教程:

好吧,如果你找到一个jQuery解决方案,你可以很容易地将它翻译成JavaScript——jQuery毕竟是JavaScript。@KevinB是的,它是重复的。我将标记它。现在我已经设置了服务器,但它不工作。客户端请求并得到响应,但响应中没有任何内容。我在
// Load the http and fs (filesystem) modules
var app = require("http");
var fs = require("fs");

// Serve the "/index.html" home page on port 8080
app.createServer(function (req, resp)
{
  fs.readFile("index.html", function(err, data)
  {
    resp.writeHead(200, {'Content-Type': 'text/html'});
    resp.write(data);
    resp.end();
  }
  );
}
).listen(8080);

// Also answer to GET requests on "/api"
app.get('/api', function(req, resp)
{
  var responseStr = "Hello World!";
  resp.status(200);
  resp.setHeader('Content-type', 'text/plain');
  return resp.send(responseStr);
}
);