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?
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);