简体   繁体   English

NodeJS错误:ENOENT,状态为'D:\\ site_deploy \\ hsoft \\ [object Object]'

[英]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 嗨,我正在尝试为我的聊天应用程序尝试nodeJS,Socket.io,express并陷入错误,一直尝试尝试在stackoverflow上发现的几种方法,这些方法与我的问题相同,但没有运气..每当我尝试加载php时页面它页面给我这个错误

Error: ENOENT, stat 'D:\\site_deploy\\hsoft[object Object]' 错误:ENOENT,状态为“ D:\\ site_deploy \\ hsoft [object Object]”

here is the index.js 这是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. 我使用php-node作为我的php解释器,这是必需的,因为我的设计模板是php格式的。 Please help 请帮忙

You get this error because validator is an object, not a string. 您收到此错误,因为validator是一个对象,而不是字符串。 Documentation says that spawn returns a ChildProcess object , which you should use to get an output of the command. 文档spawn返回一个ChildProcess object ,您应该使用该ChildProcess object来获取命令的输出。

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. 如果希望节点提供静态文件(图像,css,js),则还应添加静态中间件。 Suppose all your static files are in the /public directory. 假设所有静态文件都在/public目录中。 Add this line before app.get : app.get之前添加以下行:

app.use('/public', require('express').static(__dirname + '/public'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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