Kim Oliveros
Kim Oliveros

Reputation: 711

loading the php file on node

Hi I'm trying to develop a chat system using socket.io, express.io and node.js everything is going smoothly, and I have been following the documentation from them.

The problem is as soon as I'm trying to integrate the design to the skeleton app that I have developed, it wont load correctly. It only gives me a popup notifying me to either download or open the PHP file.

here is my index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
  res.sendfile('message.php');
});

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

Am I doing something wrong here?

Upvotes: 3

Views: 2483

Answers (1)

sampathsris
sampathsris

Reputation: 22280

socket.io sendfile will just send the contents of the file to the client. It does not matter if you give a PHP script or an image file to sendfile. It does the same.

If you want to execute a PHP script, you can do one of the following:

  • Run a separate web server that interprets PHP, and then connect to that server from Node.js, as described in this answer.
  • Run the PHP interpreter with child_process, then send the standard output of that process to client.

Upvotes: 2

Related Questions