[英]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.我想创建一个实时应用程序,但我是 Node.js、Express 和 Socket.io 的新手。 I would like to connect with Express to a file called
index.php
.我想用 Express 连接到一个名为
index.php
的文件。 Unfortunately I saw that Express connects by default to a file called index.html
.不幸的是,我看到 Express 默认连接到一个名为
index.html
的文件。
This is a part of my index.js file:这是我的index.js文件的一部分:
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 :这是我的索引。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.如您所见,我使用了一种解决方法来连接到 php 文件。 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.所以
index.html
应该重定向到index.php
但由于某种原因,浏览器只下载index.php
并且不呈现该文件。
Can help?可以帮忙?
Ciao Michele,乔米歇尔,
you can try with a PHP interpreter for NodeJS, it is called php-express.您可以尝试使用 NodeJS 的 PHP 解释器,它称为 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 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);
window.location
window.location
example of your index.html您的索引示例.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比您的
index.php
文件的示例
<?php
echo "Hello World";
?>
as finally run your express web server with:最后运行您的 express web 服务器:
node index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.