0

Using the simple chat tutorial from the socket.io website I set up a server with nodejs and socket.io, but I wanted to be able to use it with my preexisting webpage, which has php in it and is therefor a .php file. When I changed the .html to .php I was not able to get the php file loaded like the html file does. I get: Error: ENOENT, stat '/index.php' Any ideas?

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.php');
});

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');
});
Blubberguy22
  • 1,344
  • 1
  • 17
  • 29

1 Answers1

1

ENOENT is Error NO ENTry that mean your file doesn't exist. You wrote:

res.sendfile(__dirname + '/index.html');

But I think you want to do :

res.sendfile(__dirname + '/index.php');

If you change extansion file, you must to update filename in folder and in your code (js file).


But Nodejs can't execute your php code (you have to run php like a command with context argument).

How to integrate nodeJS + Socket.IO and PHP?

user2226755
  • 12,494
  • 5
  • 50
  • 73