必承其重 | 欲带皇冠 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 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿