我有一个已部署的应用程序,并且有一条路由不起作用。我收到错误消息:
“获取https:// localhost:8080 / api / contacts / all net :: ERR_CONNECTION_REFUSED”
但是,我的其他路线仍然有效,并且希望我以完全相同的方式进行设置。谁能看到这里的事吗?我已经无数次重写了这个东西,并从工作版本中剪切并粘贴了代码。什么都行不通......
该组件运行良好,两个获取调用都可以很好地检索数据:
const API_URL = "https://ppr-team.com/api";
componentDidMount() {
fetch(API_URL + '/clients/all')
.then(res => {
if(!res.ok){
throw new Error();
}
return res.json();
})
.then((result) => {
this.setState({
clients: result.sort((a, b) => a.client.localeCompare(b.client)),
});
console.log(result);
})
.catch(error => {
console.log(error);
})
.then(
fetch(API_URL + `/policy/all`)
.then(res => {
if (!res.ok) {
throw new Error();
}
return res.json();
})
.then((result) => {
this.setState({ policies: result });
console.log(result);
})
.catch(error => {
console.log(error);
})
);
}
但是,此组件给了我上面列出的连接错误,我也不确定为什么它要查看Localhost而不是域名。如您所见,get client / all调用与有效的代码完全相同。为什么这样可以在一个地方工作,而在另一个地方却给我一个连接错误?
const API_URL = "https://ppr-team.com/api";
componentDidMount() {
fetch(API_URL + '/clients/all')
.then(res => {
if (!res.ok) {
throw new Error();
}
return res.json();
})
.then((result) => {
this.setState({
clients: result.sort((a, b) => a.client.localeCompare(b.client)),
});
console.log(result);
})
.catch(error => {
console.log(error);
})
.then(
fetch(API_URL + `/contacts/all`)
.then(res => {
if (!res.ok) {
throw new Error();
}
return res.json();
})
.then((result) => {
this.setState({ contacts: result });
console.log(result);
})
.catch(error => {
console.log(error);
})
);
}
如果需要,这是我的代码,其中包含路由和连接代码:
const express = require('express');
const mysql = require('mysql2');
const cors = require('cors');
const connection = mysql.createPool({
connectionLimit: 10,
host : 'localhost',
user : 'XXXXXXXXXX',
password : 'XXXXXXXX',
database : 'XXXXXXXX'
});
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cors());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.get('/clients/all', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query("SELECT * FROM clients", function (error, results) {
// If some error occurs, we throw an error.
if (error) throw error;
console.log(results);
res.json(results);
});
connection.release();
});
});
app.get('/contacts/all', function (req, res) {
connection.getConnection(function (err, connection) {
connection.query("SELECT * FROM contacts", function (error, results) {
if (error) throw error;
res.json(results);
});
connection.release();
});
});
app.listen(8080, "0.0.0.0", () => {
console.log('Listening on port http://localhost:8080');
});
任何想法都会受到赞赏。