必承其重 | 欲带皇冠 2013-02-17 00:28 采纳率: 0%
浏览 57

node.js将数据发送到客户端?

I wonder how can I send data from node.js to client?

example node.js code -

var http = require('http');

var data = "data to send to client";

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World
");
}).listen(8125);

Now, I want to send the data variable to client and log it with JavaScript..
How can I do that?

Thanks ;)

EDIT: Does anyone know how to send array?

  • 写回答

1条回答

  • weixin_33725807 2013-02-17 00:34
    关注

    If You Want to do it after response.end you should use Socket.io or Server Send Events.

    If you want it before res.end, you would make your code look like:

    var http = require('http');
    
    var data = "data to send to client";
    
    var server = http.createServer(function (request, response) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write(data); // You Can Call Response.write Infinite Times BEFORE response.end
        response.end("Hello World
    ");
    }).listen(8125);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器