Express.js 对本地主机上 php 内置服务器的管道请求导致 ECONNREFUSED
Express.js piping request to php built-in server on localhost results in ECONNREFUSED
我正在使用 npm 构建一个简单的网站进行开发,它由提供 php 支持的提供商托管。
使用 php 的唯一功能是发送电子邮件的联系表。剩下的很简单 html 和 javascript.
我在开发中使用一个简单的 php 服务器,从 php -S localhost:8000
开始测试一个简单的 php 电子邮件脚本,然后在开发中我再次将 email.php
的代理请求反向到这个php本地服务器。
Node 应用程序在端口 3000 上,php 服务器在端口 8000 上。问题是当请求通过 localhost:3000/ 时,我使用以下快速服务器配置收到连接拒绝错误email.php:
#!/usr/bin/env node
var express = require('express');
var app = express(),
request= require('request'),
port = +(process.env.PORT || 3000);
app.set('case sensitive routing', false);
app.post( '/email.php', function( req, res ){
req.pipe( request({
url: 'http://localhost:8000/email.php',
qs: req.query,
method: req.method
}, function(error){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
// other request handlers here
app.listen(port, function() {
console.log('listening');
});
Php 服务器确实已启动并为端口 8000 上的所有页面提供服务,我可以使用浏览器浏览这些页面。我用 curl 对其进行了测试,当使用 curl.
直接发布到 localhost:8000 时,它似乎可以很好地处理请求
不知道为什么会出现这个错误,摸不着头脑,想不出任何原因。
非常感谢您的帮助。
我知道那是什么了,哦!好吧,我会 post 答案,以防其他人偶然发现这个问题。
PHP 似乎是罪魁祸首;使用 ss -ltn
检查监听端口的套接字(我在 Linux,这可能不适合你)我意识到 php 服务器只监听 IPv6。相关输出如下:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 ::1:8000
通过相关搜索,我在用户注释 post 下的 Web 服务器文档页面上找到了答案。参见 post here。解决方案是使用 127.0.0.1 而不是 localhost:
As it turned out, if you started the php server with "php -S
localhost:80" the server will be started with ipv6 support only!
To access it via ipv4, you need to change the start up command like
so: "php -S 127.0.0.1:80" which starts server in ipv4 mode only.
我正在使用 npm 构建一个简单的网站进行开发,它由提供 php 支持的提供商托管。
使用 php 的唯一功能是发送电子邮件的联系表。剩下的很简单 html 和 javascript.
我在开发中使用一个简单的 php 服务器,从 php -S localhost:8000
开始测试一个简单的 php 电子邮件脚本,然后在开发中我再次将 email.php
的代理请求反向到这个php本地服务器。
Node 应用程序在端口 3000 上,php 服务器在端口 8000 上。问题是当请求通过 localhost:3000/ 时,我使用以下快速服务器配置收到连接拒绝错误email.php:
#!/usr/bin/env node
var express = require('express');
var app = express(),
request= require('request'),
port = +(process.env.PORT || 3000);
app.set('case sensitive routing', false);
app.post( '/email.php', function( req, res ){
req.pipe( request({
url: 'http://localhost:8000/email.php',
qs: req.query,
method: req.method
}, function(error){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
// other request handlers here
app.listen(port, function() {
console.log('listening');
});
Php 服务器确实已启动并为端口 8000 上的所有页面提供服务,我可以使用浏览器浏览这些页面。我用 curl 对其进行了测试,当使用 curl.
直接发布到 localhost:8000 时,它似乎可以很好地处理请求不知道为什么会出现这个错误,摸不着头脑,想不出任何原因。
非常感谢您的帮助。
我知道那是什么了,哦!好吧,我会 post 答案,以防其他人偶然发现这个问题。
PHP 似乎是罪魁祸首;使用 ss -ltn
检查监听端口的套接字(我在 Linux,这可能不适合你)我意识到 php 服务器只监听 IPv6。相关输出如下:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 ::1:8000
通过相关搜索,我在用户注释 post 下的 Web 服务器文档页面上找到了答案。参见 post here。解决方案是使用 127.0.0.1 而不是 localhost:
As it turned out, if you started the php server with "php -S localhost:80" the server will be started with ipv6 support only!
To access it via ipv4, you need to change the start up command like so: "php -S 127.0.0.1:80" which starts server in ipv4 mode only.