如何在同一个端口上使用node.js和php [英] how to use node.js and php on the same port

查看:147
本文介绍了如何在同一个端口上使用node.js和php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在端口12234上安装了node.js,socket.io和express.js,并且工作非常好。但现在我想在该端口上的页面上使用php(12234),但是我不知道我该怎么做。有人有解决方案吗?

I've installed node.js, socket.io, and express.js on port 12234 and that works very nice. But now I want to use php on an page on that port (12234) but I don't know how I can do that. Has anybody a solution?

抱歉我的英文不好,我是荷兰,英文不太好。

sorry for my bad english, I'm Netherlands and not really good in english.

推荐答案

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

You can't run two applications on the same port. The easiest thing to do is proxy HTTP requests to PHP, running on another port, and proxy other requests to Socket.IO, running on another port.

这里是一个使用示例 HTTP代理。请注意,这将不适用于诸如flashsockets和XHR长轮询的事情。

Here's an example using http-proxy. Do note that this won't work for things like flashsockets and XHR long polling.

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

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

Alternatively, you could route your Express routes to PHP. If PHP was running on port 8080 for example:

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

这篇关于如何在同一个端口上使用node.js和php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆