如何在同一个端口上使用node.js和php

use*_*686 2 php port node.js express socket.io

我在端口12234上安装了node.js,socket.io和express.js,这非常好用.但是现在我想在该端口的页面上使用php(12234),但我不知道如何做到这一点.有人有解决方案吗?

抱歉我的英语不好,我是荷兰人,英语不太好.

hex*_*ide 6

您无法在同一端口上运行两个应用程序.最简单的做法是代理对PHP的HTTP请求,在另一个端口上运行,以及代理对另一个端口上运行的Socket.IO的其他请求.

这是一个使用的例子http-proxy.请注意,这不适用于flashsockets和XHR长轮询等内容.

var httpProxy = require('http-proxy')

var server = httpProxy.createServer(function (req, res, proxy) {
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: // whatever port PHP is running on
  });
})

server.on('upgrade', function (req, socket, head) {
  server.proxy.proxyWebSocketRequest(req, socket, head, {
    host: 'localhost',
    port: // whatever port Socket.IO is running on
  });
});

server.listen(80);
Run Code Online (Sandbox Code Playgroud)

或者,您可以将Express路由路由到PHP.例如,如果PHP在端口8080上运行:

app.get('/', function(req, res) {
  // send a HTTP get request
  http.get({hostname: 'localhost', port: 8080, path:'/', function(res2) {
    // pipe the response stream
    res2.pipe(res);
  }).on('error', function(err) {
    console.log("Got error: " + err.message);
  });
});
Run Code Online (Sandbox Code Playgroud)