【问题标题】:How do i make my server run and receive messages in real time with NodeJS? [closed]如何使用 NodeJS 使我的服务器实时运行并接收消息? [关闭]
【发布时间】:2022-01-26 14:55:46
【问题描述】:

我可以创建一个 NodeJS 服务器,但它看起来没有实时接收消息。我有一个设备可以向我创建的这个服务器发送消息,我只想在每次收到响应时“console.log”。就是这么简单。 也就是说,重要的是要注意我无法控制向我发送数据的服务器。我还没有创建它,我无法触摸它。它是外部的。但我知道它正在将数据发送到我的服务器,我只是不知道如何实时接收它....

我现在的代码就是这个。如果您愿意,可以随意更改它并完全重做它,我在 NodeJS 方面不是很有经验。也可以随意使用任何其他库,因为我尝试使用许多其他库都没有成功。

import http from 'http';

const host = 'MY.IP';
const port = 0000;

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end("--SERVER ONLINE.");
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`Server is running on http://${host}:${port}`);
});

编辑:我相信我没有正确解释:我需要打印从上面代码中的其他服务器接收到的数据。 上面的代码创建服务器 A服务器 A 正在从 服务器 B 接收数据。 我需要打印 server B 发送到server A 的数据。 服务器 B 正在通过 HTTP POST 请求发送数据。

EDIT2:我已经更改了我的代码,并且我正在接近做我需要的事情,但仍然没有成功。现在我有:

import http from 'http';
const PORT = process.env.PORT || 8000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('Server online!');
        console.log(request);
});

httpServer.listen(PORT, () => console.log(`Server running at ${PORT}`));

每次确实有对服务器的请求时,都会在我的屏幕上打印该请求。我可以看到 服务器 B 正在将数据发送到我创建的这个服务器,工作正常并且数据 100% 被发送。我只需要“console.log”我现在收到的数据。还是没有成功。

【问题讨论】:

  • 还有什么问题?你用浏览器或邮递员试过吗?你的“实时”是什么意思你的代码应该可以工作。如果您使用 http 请求访问服务器,您将收到“--SERVER ONLINE”。
  • 是的,它有效,但我相信我没有正确解释。我想打印从其他服务器接收的数据。假设上面代码中的服务器是服务器 A,它正在从服务器 B 接收数据。我想打印服务器 A 从服务器 B(在服务器 A 上)实时接收的数据。
  • 服务器B如何发送数据?通过 HTTP Post 请求?通过 HTTP Put 请求。使用普通的 TCP 套接字?通过 XMPP?没有足够的信息来回答您的问题。
  • 通过 HTTP 发布请求。我会更新我的问题。我完全忘记了这一点,
  • @Master 为什么需要服务器B?只需让网页执行 AJAX 发布请求以获取数据,然后 console.log 即可

标签: javascript node.js


【解决方案1】:

您需要使用<httpserver>.on('request', callback) 监听发送到您的服务器的请求

试试这样的

import http from 'http';
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('reached server!');
});

httpServer.listen(PORT, () => console.log(`running on port ${PORT}`));

在本例中,转到http://localhost:3000/ 应该会显示“已到达服务器!”

【讨论】:

  • 您好,谢谢您的回答。该代码确实有效,但问题是,我现在需要做的是打印您创建的此服务器正在接收的消息。因此,假设您发布的代码是服务器 A,而服务器 B 正在发送数据。我现在想在服务器 B 上回显来自服务器 B 的数据
  • @Master 但如果服务器 B 正在发出 http POST 请求,那么我所能做的就是发回一个响应对象(或消息),然后由服务器 B 的所有者处理该消息并做他想做的事
  • 我无法在服务器 A 上获取通过 HTTP POST 发送的数据?
【解决方案2】:

我自己找到了解决方案。花了我将近 2 天的时间,因为我之前没有 NodeJS 的经验,但这里是答案。我希望它对将来的人有所帮助:

import http from 'http';
const PORT = process.env.PORT || 8000;
const httpServer = http.createServer();


httpServer.on('request', (request, response) => {
    if (request.url == '/')
        response.end('Server online!');
request.on('data',function(data){
         console.log(data.toString());
});
      });



httpServer.listen(PORT, () => console.log(`Server running at: ${PORT}`));

我缺少的部分是这个:

request.on('data',function(data){
             console.log(data.toString());
    });

我不知道我可以在“请求”上使用“开启”事件,因为“请求”本身已经是一个事件。但我可以,这解决了我的问题。

现在服务器 A 正在实时接收和打印来自 服务器 B 的每条消息,我可以对响应做任何我想做的事情!

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多