【发布时间】:2019-02-18 22:33:30
【问题描述】:
我正在尝试向某个外部网站创建GET request,以最终获取一些信息。我的axios GET request 导致连接错误,我认为这可能是因为我从运行在localhost 上的express 服务器发出请求。我在我的package.json 中设置了一个代理地址到"proxy": "http://localhost:8000",,并在我的服务器中安装并需要cors。在错误对象port 属性中,值是80,尽管服务器在8000 上运行。
我目前只是在本地机器上测试路由,但是当这些请求来自部署在生产环境中的服务器时,我会遇到这个问题吗?
感谢您的帮助。
server.js
const express = require('express');
const path = require('path');
const cors = require('cors');
const axios = require('axios');
const app = express();
const port = process.env.PORT || 8000;
// MiddleWare
app.use(cors());
app.use('/', express.static(path.join(__dirname, './client/public')));
app.get('/url', (req, res) => {
const url = req.query.url;
axios
.get(url)
.then(html => {
res.send(html);
})
.catch(err => {
console.log(err);
});
});
app.listen(port, () => console.log(`Server doin it's thing on port ${port}...`));
axios 错误对象
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1158:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.18.0' },
method: 'get',
url: 'www.marlindalpozzo.com',
data: undefined },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: 'www.marlindalpozzo.com',
method: 'get',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: null,
port: null,
nativeProtocols: [Object],
pathname: 'www.marlindalpozzo.com' },
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header:
'GET www.marlindalpozzo.com HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: axios/0.18.0\r\nHost: localhost\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: 'www.marlindalpozzo.com',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http:www.marlindalpozzo.com' },
response: undefined }
【问题讨论】:
-
你试过
https:www.marlindalpozzo.com吗?我不确定你是否需要这个端口,应该是443。 -
看起来您正在尝试获取
http://localhost/www.marlindalpozzo.com。您用来调用 Express 服务器的 URL 是什么? -
我正在调用我当前在本地主机上的 express 服务器,所以我猜它认为 url 是 localhost 基本 url 之上的扩展...输入前缀为 https:// 的 url 工作...
-
@MarlyMarMar 我认为这是因为 HTTP 301 重定向到 HTTPS。您可以在浏览器中看到这种情况。
标签: javascript http express axios