简体   繁体   English

Nodejs 和 socket.io 使用 index.php 而不是 index.html

[英]Nodejs and socket.io using index.php instead of index.html

Using the simple chat tutorial from the socket.io website I set up a server with nodejs and socket.io, but I wanted to be able to use it with my preexisting webpage, which has php in it and is therefor a .php file.使用来自 socket.io 网站的简单聊天教程,我使用 nodejs 和 socket.io 设置了一个服务器,但我希望能够将它与我预先存在的网页一起使用,其中包含 php,因此是一个 .php 文件。 When I changed the .html to .php I was not able to get the php file loaded like the html file does.当我将 .html 更改为 .php 时,我无法像 html 文件那样加载 php 文件。 I get: Error: ENOENT, stat '/index.php' Any ideas?我得到:错误:ENOENT,stat '/index.php' 有什么想法吗?

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.php');
});

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

ENOENT is Error NO ENTry that mean your file doesn't exist. ENOENTError NO ENTry ,这意味着您的文件不存在。 You wrote:你写了:

res.sendfile(__dirname + '/index.html');

But I think you want to do :但我认为你想做:

res.sendfile(__dirname + '/index.php');

If you change extansion file, you must to update filename in folder and in your code (js file).如果更改扩展文件,则必须更新文件夹和代码(js 文件)中的文件名。


But Nodejs can't execute your php code (you have to run php like a command with context argument).但是 Nodejs 无法执行您的 php 代码(您必须像运行带有上下文参数的命令一样运行 php)。

How to integrate nodeJS + Socket.IO and PHP? 如何集成nodeJS + Socket.IO 和PHP?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM