-1

I receive this error in console GET http://localhost/socket.io/socket.io.js 404 (Not Found). I used npm install to install express and socket.io.
Everytime I try to access localhost:3000 it downloads a file instead of displaying chat.php
This is my javascript code

var express = require('express')
var app = express()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server);

server.listen(3000);

users = [];
connnection = [];

console.log('Server running!');

app.get('/',function(req, res){
    res.sendFile(__dirname + '/game.php');
});

io.sockets.on('connection', function(socket){
  connections.push(socket);
  console.log('Connected: %s sockets connected', connections.length);

  //Disconnect
  socket.on('disconnect', function(data){
  connections.splice(connections.indexOf(socket),1);
  console.log('Disconnected: %s sockets connected', connections.length);
});
});

And this is what I added into php file

<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
    var socket=io.connect();
});
</script>
Daniel
  • 57
  • 1
  • 8
  • What are you expecting to happen with you do `res.sendFile(__dirname + '/game.php')`? Do you understand that PHP does not run in the user's browser, but only runs on the server so sending a PHP file to a browser (which is what you are doing) will not do something useful? What do you want to happen when you do `res.sendFile(__dirname + '/game.php');`? – jfriend00 Mar 16 '17 at 21:30
  • @jfriend00 I am new to this socket.io and I was following a tutorial. In that tutorial when accessing localhost:3000 it was redirected to chat.html – Daniel Mar 16 '17 at 21:34
  • I think I see... When you normally go to (for example) www.example.com/page.php you see the web page, and it appears to work the same as if you had went to www.example.com/page.html But the php url actually returns an HTML interpretation of the php file. In this example, I don't believe that this will work because node.js won't know to run your `game.php` through an interpreter first. If you rename it `game.html` it should work as expected, since there is no php code in your file. – Michael S Mar 16 '17 at 21:41
  • What is in game.php? Is it a PHP file or an HTML file? Can you show its contents in your question? – jfriend00 Mar 16 '17 at 21:42
  • @jfriend00 It is a php file – Daniel Mar 16 '17 at 21:43
  • If your question has been answered now, then please mark one of the answers as the accepted answer by clicking the green checkmark next to it. You can accept your own answer if that's your choice. – jfriend00 Mar 24 '17 at 04:12

2 Answers2

0

res.sendFile(__dirname + '/game.php'); just sends a raw PHP file to the browser client. Instead, what you need to send to the browser is HTML. So, you either have to change your app to run in node.js and not PHP or you have to exec that PHP file and grab its output and send that to the browser.

Normally, if you wanted your page to be generated via PHP, you wouldn't be using node.js at all - you'd just be using PHP. If the only reason you brought node.js into the equation is because of socket.io, then maybe you should be using socket.io directly with PHP which you can read about in this question. You could use a hybrid of node.js and PHP, but it's unlikely to be all that efficient if you're using node.js to run your PHP. For that case, you would probably be better off running socket.io in node.js on a different port number, enabling cross origin access and just leaving your PHP to be PHP.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

I found the working solution with PHP here https://github.com/jdutheil/nodePHP. I tested it and it is working just great.

Daniel
  • 57
  • 1
  • 8