简体   繁体   中英

NodeJS Error: ENOENT, stat 'D:\site_deploy\hsoft\[object Object]'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM