Change file client root using Express and Node.js to a .php file

I am doing a RTA project using Express, Node.js and Socket.io. I am a newbe in those technologies.

This is my code:

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

The above code works fine. The only problem is that I must connect to a file called html.php (or also another name… but .php).
Unfortunately with the above code it looks like the root of the client is always index.html.
Can help?

Hey Michael,

nice to meet you! :wave:

Seems you want to run an express server.
Where do you currently run this server? Locally?

An express server listens for requests.
Where do these requests come from?
Do you have a frontend application?

Hi Miku,

I run an Apache server locally with Xampp.

This is my index.html under the public folder:

<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>
<button id="send">Send</button>

<div id="table_container">

</div>

<script src="/receiver.js"></script>

This is my receiver.js file:

//Make connection

var socket = io.connect(‘http://localhost:4000’);

var btn = document.getElementById(‘send’);

socket.on(‘connect’, function (data) {

socket.emit('sendCustomerInfo', { customerId: 555555, permissionId: 1 });

});

//Emit events

btn.addEventListener(‘click’, function(){

socket.emit('changeStatus', 'may I have a log?');

});

socket.on(‘my message’, function (message) {

console.log(message);

});

socket.on(‘query_res’, function(data){

console.log(data);

console.log(Object.keys(data[0]));

let oldTable = document.getElementById('call_table');

if(oldTable){

    let rowFormat = 'call_';

    let currentRows = findCurrentRows();

    let futureRows = findNewRows(data);

    let idsToAdd = findNewIds(currentRows, futureRows);

    let idsToRemove = findOldIds(currentRows, futureRows);

    //tests

    /*idsToRemove.push(85, 67);

    idsToAdd.push(666);

    idsToAdd.push(777);

    data.push({"prov_id": 666, "prov_nome": "DI"});

    data.push({"prov_id": 777, "prov_nome": "DC"});

    console.log(data);

    console.log(currentRows);

    console.log(futureRows);

    console.log(idsToAdd);

    console.log(idsToRemove);*/

    if(idsToRemove.length > 0) {

        removeRows(idsToRemove, rowFormat);

    }

    if(idsToAdd.length > 0) {

        addRows(data, 'call_table', idsToAdd);

    }

            

} else {

    createCallTable(data);

}

});

findCurrentRows = () => {

let currentRowsId = [];

let table = document.getElementById("call_table");

let rows = table.getElementsByTagName("tr");

for(let i=0;i<rows.length;i++) {

    currentRowsId.push(rows[i].id);

}

return findIdFromArr(currentRowsId);

}

findNewRows = (newData) => {

const numberRowsData = newData.length; 

let currentRowsDataId = [];

for(let i=0;i<numberRowsData;i++) {

    currentRowsDataId.push(newData[i]['prov_id']);

}

return currentRowsDataId;

}

findIdFromArr = (arrToSplit) => {

let rowsId = [];

let idTemp = "";

for(let i=0;i<arrToSplit.length;i++) {

    idTemp = arrToSplit[i].split("_");

    rowsId.push(parseInt(idTemp[1]));

}

return rowsId;

}

findNewIds = (currentRows, futureRows) => {

let newIds = [];

newIds = futureRows.filter(d => !currentRows.includes(d));

return newIds;

}

findOldIds = (currentRows, futureRows) => {

let oldIds = [];

oldIds = currentRows.filter(d => !futureRows.includes(d));

return oldIds;

}

removeRows = (arrToRemove, rowFormat) => {

let removeArr = arrToRemove;

let strRow = rowFormat;

for(let i=0; i < removeArr.length; i++) {

    let rowId = strRow.concat(removeArr[i]);

    let oldRow = document.getElementById(rowId);

    if(oldRow) {

        oldRow.parentNode.removeChild(oldRow);

    }

    

}

}

addRows = (data, tableName, arrToAdd) => {

const table = document.getElementById(tableName);

const arrId = arrToAdd;

const newRowsData = data.filter(function(item){

    return arrId.includes(item.prov_id);       

});

/*console.log(newRowsData[0]['prov_id']);

console.log(newRowsData[0]['prov_nome']);*/

for(let i=0; i < newRowsData.length; i++) {

    let row = table.insertRow(0);

    row.setAttribute("id", "call_" + newRowsData[i]['prov_id']);

    let td1 = row.insertCell(0);

    let td2 = row.insertCell(1);

    let provId = document.createTextNode(newRowsData[i]['prov_id']);

    td1.appendChild(provId);

    let provNome = document.createTextNode(newRowsData[i]['prov_nome']);

    td2.appendChild(provNome);

}

}

createCallTable = (data) => {

const numberRows = data.length; 

const numberColumns = Object.keys(data[0]).length;

const col1 = "ID";

const col2 = "Name";

var tc = document.getElementById('table_container');

var tbl = document.createElement('table');

tbl.setAttribute("id", "call_table");

tbl.style.width = '100%';

var tbdy = document.createElement('tbody');

var th1 = document.createElement('th');

th1.appendChild(document.createTextNode(col1))

var th2 = document.createElement('th');

th2.appendChild(document.createTextNode(col2))

tbdy.appendChild(th1);

tbdy.appendChild(th2);

for (let i = 0; i < numberRows; i++) {

    var tr = document.createElement('tr');

    

    for (let j = 0; j < numberColumns; j++) {

        tr.setAttribute("id", "call_" + data[i]['prov_id']);

        var td = document.createElement('td');

        if(j === 1) {

            td.appendChild(document.createTextNode(data[i]['prov_nome']))

        } else {

            td.appendChild(document.createTextNode(data[i]['prov_id']))

        }

        

        tr.appendChild(td);

    }

    tbdy.appendChild(tr);

}

tbl.appendChild(tbdy);

tc.appendChild(tbl)

}

I want to create an app with Express that sends to a PHP application query results using Socket.io. Every client that is connected with the web app should see only some query results depending on their permissions. Currently I am doing only some tests and my current aim is to send the query results to a file with php extension but I don’t know how to do that.