NodeJS Error: ENOENT, stat 'D:\site_deploy\hsoft\[object Object]' - node.js

Hi I was trying out the nodeJS, Socket.io, express for my chat application and got stucked on an error been trying out few methods found on stackoverflow who has the same issue as mine but no luck.. whenever I try to load the php page it the page gives me this error
Error: ENOENT, stat 'D:\site_deploy\hsoft[object Object]'
here is the index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var spawn = require('child_process').spawn;
var validator = spawn('php', ['message.php']);
app.get('/', function(req, res){
res.sendfile(__dirname + '/' +validator);
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I using php-node as my php interpreter It is needed because my design template is on php format. Please help

You get this error because validator is an object, not a string. Documentation says that spawn returns a ChildProcess object, which you should use to get an output of the command.
You may use this simple function to get an output:
function getStdout(command, args, fn) {
var childProcess = require('child_process').spawn(command, args);
var output = '';
childProcess.stdout.setEncoding('utf8');
childProcess.stdout.on('data', function(data) {
output += data;
});
childProcess.on('close', function() {
fn(output);
});
}
Then use it in your code:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var validator;
app.get('/', function(req, res){
//res.sendfile(__dirname + '/' +validator);
res.send(validator);
});
//you should have only one io.on('connection')
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
});
getStdout('php', ['message.php'], function(output) {
validator = output;
//start your server after you get an output
http.listen(3000, function(){
console.log('listening on *:3000');
});
});
Update:
If you want node to serve static files (images, css, js) you should also add static middleware. Suppose all your static files are in the /public directory. Add this line before app.get:
app.use('/public', require('express').static(__dirname + '/public'));

Related

How to get Server connected?

I'm trying to build a chat app from a Youtube tutorial and I can't get 'Server connected', only "Server running".
My project is here https://danclaudiu95#bitbucket.org/danclaudiu95/chat-app-io.git
The server.js contains:
var express = require('express'),
app = express(),
io = require('socket.io').listen(app);
usernames = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.listen(process.env.PORT || 3000);
console.log('Server Running...');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket){
console.log('Socket Connected...');
socket.on('new user', function(data, callback){
if(usernames.indexOf(data) != -1){
callback(false);
} else {
callback(true);
socket.username = data;
usernames.push(socket.username);
updateUsernames();
}
});
// Update Usernames
function updateUsernames(){
io.sockets.emit('usernames', usernames);
}
// Send Message
socket.on('send message', function(data){
io.sockets.emit('new message', {msg: data, user:socket.username});
});
// Disconnect
socket.on('disconnect', function(data){
if(!socket.username){
return;
}
usernames.splice(usernames.indexOf(socket.username), 1);
updateUsernames();
});
});
Can anyone point me what am I doing wrong?
On server.js file, the line "var express = require('express')," is dotted at the word "require".
Remove requiring http. Express already do that for you.
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
usernames = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.listen(process.env.PORT || 3000);
console.log('Server Running...');
//.. The rest of your code

NodeJS include functions

Lets say I have the following NodeJS file:
var https = require("https");
var express = require("express");
var app = express();
var options = {};
var serverPort = 8443;
var server = https.createServer(options, app);
var io = require('socket.io')(server);
var numUsers = 0;
app.get('/', function(req, res){
res.sendFile('/home/domain/index.php');
});
io.on('connection', function(socket){
socket.on('user-login', function(data){
++numUsers;
});
socket.on('new message', function (msg,room) {
console.log(msg);
});
socket.on("disconnect", function() {
--numUsers;
});
});
server.listen(serverPort, function(){
console.log("\n--------------------------------");
console.log('Node HTTPs Server');
console.log('Currently Listening on port %d',serverPort);
console.log("--------------------------------");
});
Since I can't get SNI to work on my server, I'll have to go the old fashioned way and write a script for each subdomain. But what I'd like to do is have the functions inside of the io.on('connection', function(socket) {} area to be included. So not included like a class or anything like that, but literally the code is just taken from another file and processed as if it were in that file already. A lot like PHP does includes. Is this possible?
Simplest solution would be to read code using fs.readFile[Sync] and pass it to eval inside io.on('connection', function(socket) {})
io.on('connection', function(socket){
socket.on('user-login', function(data){
++numUsers;
});
socket.on('new message', function (msg,room) {
console.log(msg);
});
socket.on("disconnect", function() {
--numUsers;
});
// eval function loaded outside io.on('connection')
eval(someFunctionBody);
// or
eval(fs.readFileSync('path/to/function/body.js'));
});
Can't you just use require?
functions.js
function myFunc() {
console.log("I am a funky func");
}
module.exports = {
myFunc,
myOtherFunc,
};
index.js
var https = require("https");
var express = require("express");
// snip
var funcs = require('./functions');
io.on('connection', function(socket){
// snip
funcs.myFunc();
});

I'm unable to connect to socket

I'm trying to connect to a socket.But I did not get the socketid on the console.Is it the right way of connecting to a socket ?Can anyone please suggest me ...
My code :
var app = express();
var dir = process.cwd();
app.use(express.static(dir)); //app public directory
app.use(express.static(__dirname)); //module directory
var server =require('http').createServer(app);
var io = require('socket.io')(server);
io.of('/socket_issue').on('connection', function (socket) {
console.log("Socket connected :"+socket.id);
socket.emit('news', { hello: 'world' });
});
client code :
var socket = io('http://localhost:8085/socket_issue');
socket.on('connect', function(){ console.log('connected to socket'); });
socket.on('error', function(e){ console.log('error' + e); });
socket.on( 'news', function( data ){
console.log(data);
});
socket.on('disconnect', function(){});
You seem to not have a server.listen() in your backend code.
I've edited the server code and it functions correctly:
var app = require('express')();
var dir = process.cwd();
var server =require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.of('/socket_issue').on('connection', function (socket) {
console.log("Socket connected :"+socket.id);
socket.emit('news', { hello: 'world' });
});
Don't forget to change the port on the front-end and it'll work as expected:
Socket connected :Y7zi7dLRxqBA5nakAAAA

socket.io polling 404 error

I try to learn socket.io on my web server. I have installed node.js and socket.io on my server. I have prepared web site my socket javascript code is work properly -listening port 3000 on console- but when i try to run example chat application, i got "socket.io/?EIO=3&transport=polling&t=1423949334574-31 404 (Not Found)" error. Where is mistake?
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
There is my index.html script:
<script>
var socket = io("http://localhost:3000");
$('form').submit(function(){
socket.emit('chat message', $('#messagebox').val());
var mesaj = $('#messagebox').val();
$('.messageBoard').prepend('<p style="display: none">'+mesaj+'</p>');
$('#messagebox').val('');
$('.messageBoard').find('p:first-child').show('slideDown');
e.preventDefault();
return false;
});
socket.on('chat message', function(msg){
$('.messageBoard').prepend('<p style="display: none">'+mesaj+'</p>');
});
</script>

Socket Property Of Undefined

I am currently trying to make a real time chat room using node, express, and socket.io. When I run the code, I get this error message:
http://gyazo.com/d9956a80a691d1642b438173b0bd85bf
Also, here is my index.js code:
var express = require("express");
var app = express();
var port = 3700;
app.use(express.static(__dirname + "/public"));
app.set("views", __dirname + "/tpl");
app.set("view engine", "jade");
app.engine("jade", require("jade").__express);
app.get("/", function (req, res){
res.render("page");
});
io.sockets.on("connection", function (socket){
socket.emit("message", { message: "welcome to the chat" });
socket.on("send", function (data){
io.sockets.emit("message", data);
});
});
var io = require("socket.io").listen(app.listen(port));
console.log("Listening On Port " + port);
I have searched up over google and found no solutions that were related to my code. Please Help!
You are using the io variable before it's initialized move the var io before the io.sockets
var io = require("socket.io").listen(app.listen(port));
io.sockets.on("connection", function (socket){
socket.emit("message", { message: "welcome to the chat" });
socket.on("send", function (data){
io.sockets.emit("message", data);
});
});
You're using a variable before it's declared and your listen() calls look kind of strange.
Instead of doing this:
io.sockets.on("connection", function (socket){
socket.emit("message", { message: "welcome to the chat" });
socket.on("send", function (data){
io.sockets.emit("message", data);
});
});
var io = require("socket.io").listen(app.listen(port));
You should do something more like this:
var io = require("socket.io")(app);
io.sockets.on("connection", function (socket){
socket.emit("message", { message: "welcome to the chat" });
socket.on("send", function (data){
io.sockets.emit("message", data);
});
});
app.listen(port);
You are accessing the io variable before it is initialized.
Simply move the following line (io variable initialization):
var io = require("socket.io").listen(app.listen(port));
Before the first use of io variable at this line:
io.sockets.on("connection", function (socket){

Resources