0

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

Kim Oliveros
  • 711
  • 1
  • 7
  • 28

1 Answers1

1

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'));
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • Hi I tried your code and it just gave me the plain source code it didn't load the actual php script this is what my code looks like after following your suggestion http://pastebin.com/TPp99XRQ – Kim Oliveros Jul 31 '14 at 22:30
  • try to put `console.log(validator);` after `validator = output;`. what do you see in console? what `php message.php` **should** output? – Oleg Aug 01 '14 at 04:25
  • you may also try to replace `res.sendfile(__dirname + '/' +validator);` with `res.send(validator);` – Oleg Aug 01 '14 at 09:02
  • on my console.log it showed me the whole php file.. and after I changed the res.sendfile.. it did work but the image is broken and there are no css rule... :( – Kim Oliveros Aug 01 '14 at 22:58
  • Why will You los da PHP source file? Your browser is Not able to Interpret it and Node.js cannot either. IF you want to use node, forget about php. – solick Aug 02 '14 at 06:05
  • @solick I'm using a php framework and the reason why I chose node is because I think using node is faster compare to using php.. correct me If I'm wrong – Kim Oliveros Aug 02 '14 at 07:55
  • @CuriousGuy your updated answer did do the trick.. I just declared the app.use twice 1 is for the folder of the css and the other is for the images.. or Are there better ways of doing it? – Kim Oliveros Aug 02 '14 at 08:20
  • Another solution would be putting css files into `/public/css`, and images into `/public/images`.Then you could write `app.use('/public', ...)` once. But the way you did it is acceptable too. The choice is yours :) – Oleg Aug 02 '14 at 10:10
  • I see I think I'll go with your suggested format.. Its cleaner that way.. anyway thanks for the help you just saved me :)) – Kim Oliveros Aug 02 '14 at 14:01
  • @CuriousGuy the Problem with php is, that you need an instance to interpret the code, otherwise you will display your plain php code in your browser. I do not now a node package which interprets php. – solick Aug 03 '14 at 06:50
  • @solick there is no such node package, you are right. but I think it is not that hard to install php on the server, at least OP says that this solution works :) – Oleg Aug 03 '14 at 08:18
  • @CuriousGuy of course you can install a php / webserver besides your node.js installation. But what then: Call from node.js the local weberver to get the interpreted php and pass this to the node.js resonse? Sure, it sould be possible but i do not understand the benefit. Why don´t use node.js and some frameworks like angularJS or whatever and create you full solution without php? – solick Aug 04 '14 at 08:12
  • @solick I also would not use this approach, but since this is a QA site, I just suggested how OP could fix his error. – Oleg Aug 04 '14 at 08:16