I'm currently attempting to execute a GET request
on an external website in order to scrape some information. Unfortunately, my axios GET request
is returning a connection error. I suspect that this issue may be related to the fact that I am sending the request from an express
server hosted on localhost
. To address this, I have configured a proxy address in my package.json
as
"proxy": "http://localhost:8000",
, and I have also installed and included cors
in my server setup. Interestingly, within the error object, the port
property is showing a value of 80
, despite the server running on port 8000
.
At present, I am simply testing routes locally on my machine. However, I am curious if this problem will persist when these requests are made from a production-deployed server.
Thank you for any guidance.
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 operating on port ${port}...`));
Axios Error Object
{ 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...