Hello I am new in nodeJS, I am trying to broadcast message when any new request arrive.
I am trying ajax calling and send ttype variable and broadcast to all browsers but My problem is when I request arrive
socket.emit('date', {'date': ttype});
not working. but when I checked it on my nodeJs console it's show tyype and it's value. here is my code
var http = require('http');
var url = require('url');
var fs = require('fs');
var server;
var ttype = "";
server = http.createServer(function(req, res){
// your normal server code
var path = url.parse(req.url).pathname;
var queryObject = url.parse(req.url,true).query;
ttype = queryObject.type;
switch (path){
case '/':
fs.readFile(__dirname + path + "test.html", function(err, data){
if (err){
console.log('error');
//return send404(res);
}
res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'});
res.write(data, 'utf8');
res.end();
sendMessage(ttype);
});
break;
case '/test.html':
fs.readFile(__dirname + path, function(err, data){
if (err){
return send404(res);
}
res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'});
res.write(data, 'utf8');
res.end();
});
break;
default: send404(res);
}
}),
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
};
server.listen(8076);
sendMessage = function(ttype){
var iio = require('socket.io').listen(server);
iio.sockets.on('connection', function(socket){
socket.emit('date', {'date': ttype});
console.log('in side connection');
});
}
// use socket.io
var io = require('socket.io').listen(server);
// define interactions with client
io.sockets.on('connection', function(socket,data){
socket.emit('date', {'date': ttype});
setInterval(function(){
//socket.emit('date', {'date': new Date()});
}, 5000);
//recieve client data
socket.on('client_data', function(data){
process.stdout.write(data.letter);
});
});
So if you'd like to let all browsers know that there is a new request by sending them ttype. All you would have to do is change your sendMessage function to:
sendMessage = function(ttype){
io.emit('date', {'date': ttype});
console.log('in side connection');
}
Related
var http = require("http");
var fs = require("fs");
http.createServer(function (req, res) {
fs.readFile("demo1.html", function (err, data) {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(data);
return res.end();
});
}).listen(80);
Error :
You don't check for an error in the callback of readFile. If there is an error, data will be undefined and res.write(data) throws the error you see.
var http = require("http");
var fs = require("fs");
http.createServer(function (req, res) {
fs.readFile("demo1.html", function (err, data) {
if (err) {
console.log(err);
res.writeHead(404); //or whatever status code you want to return
} else {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(data);
}
return res.end();
});
}).listen(80);
I cant understand how and where to use response.end(). i get it working when loading only single response but when i try to load (say) two html files (index and form in my case) , the server does not load anything.
if i put res.end() after loading the index html it works but server closes.
var http = require('http');
var formidable = require('formidable');
var url=require('url');
var path=require('path');
var fs=require('fs');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
filepath=path.join(__dirname,'index.html');
fs.readFile(filepath,null,function(err,data){
if(err){
res.writeHead(404);
res.write(err+" Error");
}
else{
res.write(data);
}
res.end();
});
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function () {
res.write('File uploaded');
res.end();
});
} else {
var q=url.parse(req.url,true);
var filename=path.join(__dirname,q.pathname);
console.log(filename);
fs.readFile(filename,function(err,data){
if (err) {
res.writeHead(404);
res.write(err+"404 Not Found");
}
else{
res.write(data);
}
res.end();
})
}
}).listen(8080);
Other tips on improving code is much appreciated :)
var http = require('http');
var formidable = require('formidable');
var url = require('url');
var path = require('path');
var fs = require('fs');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function () {
res.write('File uploaded');
res.end();
});
} else if (req.url.length > 1) {
var q = url.parse(req.url, true);
var filename = path.join(__dirname, q.pathname);
console.log(filename);
fs.readFile(filename, function (err, data) {
if (err) {
res.writeHead(404);
res.write(err + "404 Not Found");
}
else {
res.write(data);
}
res.end();
})
} else {
filepath = path.join(__dirname, 'index.html');
fs.readFile(filepath, null, function (err, data) {
if (err) {
res.writeHead(404);
res.write(err + " Error");
}
else {
res.write(data);
}
res.end();
});
}
}).listen(8080);
I'm trying to pick up a file from a form and save it to myself on disk in any folder. I would like to do it by sockets, but I do not know how to handle it. Can someone help me?
This is my code:
main.js
var http = require('http');
var fs = require('fs');
var io = require('socket.io');
var path = require('path');
var server = http.createServer(function (req, res) {
fs.readFile(__dirname + '/index.html', function (err, data) {
if (err) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("Not Found");
res.end();
} else {
res.write(data, 'utf8');
res.end();
}
});
}).listen(8000);
var listener = io.listen(server);
listener.sockets.on('connection', function (socket) {
console.log("Connected");
socket.on('disconnect', function () {
console.log("Disconnect");
});
socket.on('form.message', function (data) {
// here to handle the file ??
socket.emit('new.message', {msg: data});
});
});
index.html(client)
<script>
$(function () {
var socket = io.connect();
var $messageForm = $('#messageForm');
var $file = $('#file');
var $outputFile = $('#outputFile');
var obj = {
file: $file.val(),
};
e.preventDefault();
console.log(obj);
socket.emit('form.message', obj);
$file.val('');
socket.on('new.message', function (data) {
$outputFile.append('<div class="well">' + data.msg.file + '</div>');
});
});
</script>
var http = require('http');
var url= require('url');
var util= require('util');
var fs = require('fs');
var body_404="<html><body><center>404 error</center></body></html>";
http.createServer(function (req,res) {
var what = url.parse("http://127.0.0.1:1235"+req.url);
var pathname = what.pathname;
switch(pathname) {
case "/":
pathname="/www/index.html";
default:
res.writeHead(200, {'Content-type' : 'text/html'});
ret = res;
fs.stat("."+pathname, function (err, stat) {
if(err)
res.write(body_404);
else
res.write(fs.readFileSync("."+pathname));
});
res.end();
break;
}
}).listen(1235, '127.0.0.1');
I am wondering why the write method inside the fs.stat callback does not actually write anything, it seems, to the client. I believe res is in scope.
You're calling res.end before res.write. Therefore, nothing gets written out. Move the call to res.end into the stat handler:
var http = require('http');
var url= require('url');
var util= require('util');
var fs = require('fs');
var path = require('path');
var body_404="<html><body><center>404 error</center></body></html>";
var rootPath = path.abspath(".");
http.createServer(function (req,res) {
var what = url.parse("http://127.0.0.1:1235"+req.url);
var pathname = what.pathname;
var buffer;
switch(pathname) {
case "/":
pathname="/www/index.html";
default:
var filename = path.join(rootPath, pathname);
if (filename.indexOf(rootPath) !== 0) {
res.writeHead(400, {'Content-type': 'text/plain'});
res.write('Directory traversal attack averted.');
res.end();
return;
}
fs.readFile(function (err, content) {
if(err) {
res.writeHead(404, {'Content-type' : 'text/html'});
res.write(body_404);
} else {
res.writeHead(200, {'Content-type' : 'text/html'});
res.write(content);
}
res.end();
});
break;
}
}).listen(1235, '127.0.0.1');
Also note that your original code is vulnerable to directory traversal attacks, and suffers from a race condition between os.stat and os.readFileSync.
I am tying to redirect a simple UDP broadcast result containing several delayed in (2 sec.) messages from different devices and render the result as a http response. The code bellow is working well and I am able to see the collected broadcast message with the console.log but the web response is allays empty. How to implement this correctly?
var dgram = require("dgram");
var http = require('http');
function broadcast(callback) {
var data = '';
var server = dgram.createSocket("udp4");
server.on("message", function (message, rinfo) {
data += message;
})
server.bind(11000);
var socket = dgram.createSocket("udp4");
socket.bind();
socket.setBroadcast(true);
socket.send(Buffer([3]), 0, 1, 11001, '255.255.255.255', function(err, bytes) {
socket.close();
});
// dealy to collect all messages in 2 sec
setTimeout(function () {
callback(data);
}, 2000);
}
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
broadcast(function(data) {
res.write(data);
console.log(data);
});
res.end();
}).listen(6969, "0.0.0.0");
console.log('HTTP server running at http://0.0.0.0:6969/')
You're calling res.end() before you've sent data. Move res.end() into your callback.
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
broadcast(function(data) {
res.write(data);
console.log(data);
res.end();
});
}).listen(6969, "0.0.0.0");