简体   繁体   中英

Nodejs and socket.io using index.php instead of index.html

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

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?

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