Skip to content
Advertisement

How to connect to index.php file with Express and Node.js

I would like to create a real time application, but I am new to Node.js, Express and Socket.io. I would like to connect with Express to a file called index.php. Unfortunately I saw that Express connects by default to a file called index.html.

This is a part of my index.js file:

var express = require('express');
var socket = require('socket.io');

var app = express();

//App setup
var server = app.listen(4000, function() {
    console.log('listening on requests on port 4000');
});

//Static files
app.use(express.static('public'));

//Socket setup
var io = socket(server);

This is my index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Socket Sender</title>
        <script src="https://cdn.socket.io/3.1.1/socket.io.min.js" integrity="sha384-gDaozqUvc4HTgo8iZjwth73C6dDDeOJsAgpxBcMpZYztUfjHXpzrpdrHRdVp8ySO" crossorigin="anonymous"></script>
    </head>
<body onload="redirect()">
    <button id="send">Send</button>
    <div id="table_container">
    </div>
    <script language=javascript>
        function redirect(){
          window.location = "index.php";
        }
    </script>
    <!--<script src="/receiver.js"></script>-->
</body>
</html>

As you can see, I used a workaround to connect to a php file. So the index.html should redirect to index.php but for some reason the browser only downloads index.php and doesn’t render that file.

Can help?

Advertisement

Answer

Ciao Michele,

you can try with a PHP interpreter for NodeJS, it is called php-express. So, first of all need to install it with:

npm install php-express

than your project structure must be:

./ index.js
--/public/index.html // <--- html file for redirect
--/views/index.php // <--- your php file

index.js

var express = require('express');
var socket = require('socket.io');
var phpExpress = require('php-express')({
    binPath: 'php'
});

var app = express();

//App setup
var server = app.listen(4000, function() {
    console.log('listening on requests on port 4000');
});


app.engine('php', phpExpress.engine);

app.all(/.+.php$/, phpExpress.router);

//Static files
app.use(express.static('public'));


//Socket setup
var io = socket(server);
  • in index.html need to append href attribute to window.location

example of your index.html

<!DOCTYPE html>
<html>
<head>
    <title>Socket Sender</title>
    <script src="https://cdn.socket.io/3.1.1/socket.io.min.js" integrity="sha384-gDaozqUvc4HTgo8iZjwth73C6dDDeOJsAgpxBcMpZYztUfjHXpzrpdrHRdVp8ySO" crossorigin="anonymous"></script>
</head>
<body onload="redirect()">
<button id="send">Send</button>
<div id="table_container">
</div>
<script language=javascript>
    function redirect(){
        window.location.href = 'index.php';
    }
</script>
<!--<script src="/receiver.js"></script>-->
</body>
</html>

than an example of your index.php file

<?php

echo "Hello World";

?>

as finally run your express web server with:

node index.js
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement