您无法在同一端口上运行两个应用程序.最简单的做法是代理对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)
| 归档时间: |
|
| 查看次数: |
1620 次 |
| 最近记录: |