【问题标题】:Nodejs and socket.io using index.php instead of index.htmlNodejs 和 socket.io 使用 index.php 而不是 index.html
【发布时间】:2014-09-06 01:13:35
【问题描述】:

使用来自 socket.io 网站的简单聊天教程,我使用 nodejs 和 socket.io 设置了一个服务器,但我希望能够将它与我预先存在的网页一起使用,其中包含 php,因此需要一个 . .php 文件。当我将 .html 更改为 .php 时,我无法像 html 文件那样加载 php 文件。我得到:错误: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');
});

【问题讨论】:

标签: php html node.js sockets socket.io


【解决方案1】:

ENOENT 是 Error NO ENTry,这意味着您的文件不存在。你写道:

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

但我认为你想这样做:

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

如果您更改扩展文件,您必须更新文件夹和代码(js 文件)中的文件名。


但是 Nodejs 不能执行你的 php 代码(你必须像使用上下文参数的命令一样运行 php)。

How to integrate nodeJS + Socket.IO and PHP?

【讨论】:

  • 关于这个的例子?请详细说明
  • @FernandoTorres 操作员忘记将“.php”更改为“.html”:stackoverflow.com/posts/24765021/revisions
  • @A-312 问题是在询问“.php”,所以它应该保留在问题中。我不记得我是如何解决的,但你的回答很有帮助。
  • @Blubberguy22 你忘了把 .html 改成 .php
  • @A-312 老实说不记得了,可能是这样的。
猜你喜欢
  • 2012-12-21
  • 1970-01-01
  • 2012-06-29
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
相关资源
最近更新 更多