Javascript 在本地计算机外部调用express终结点时连接超时
因此,我的服务器上运行着一个mysql数据库,可以使用express和node进行访问。我遇到的问题是,当我从浏览器调用express端点时,连接超时。但是,如果服务器本身的IJavascript 在本地计算机外部调用express终结点时连接超时,javascript,mysql,node.js,express,xmlhttprequest,Javascript,Mysql,Node.js,Express,Xmlhttprequest,因此,我的服务器上运行着一个mysql数据库,可以使用express和node进行访问。我遇到的问题是,当我从浏览器调用express端点时,连接超时。但是,如果服务器本身的Iwget-ipaddress:3030/matches/正确返回数据 为什么我不能从浏览器访问端点?谢谢大家! 这就是我的expressdbconnect.js的样子 var connection = mysql.createConnection({ //DB properies.. host: "loca
wget-ipaddress:3030/matches/
正确返回数据
为什么我不能从浏览器访问端点?谢谢大家!
这就是我的expressdbconnect.js
的样子
var connection = mysql.createConnection({
//DB properies..
host: "localhost",
user: "admin",
password: "******",
socketPath:"/var/run/mysqld/mysqld.sock",
database: "conp2"
});
var port = 3030;
connection.connect(function (error) { //connect to database
if (!!error){
console.log(error);
console.log("Error Connecting to database");
} else {
console.log("Connected to port:" + port);
}
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get("/matches", function (req,res) {
res.send("example");
}
app.listen(port);
在我的html页面中,以下是我如何调用端点:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status == 200) {
//here I manipulate the data received
console.log(this.responseText);
tallier.scoreparser(this.responseText);
}
};
xhttp.open("GET", "http://localhost:3030/matches", true);
xhttp.send();
您是否在
xhtp.open
调用中尝试使用127.0.0.1
而不是localhost?是的,我尝试过,但它不起作用,但通常在http.open调用中我有服务器地址您是否在xhtp.open
调用中尝试使用127.0.0.1
而不是localhost?是的,我尝试过,但它不起作用通常在http.open调用中,我有服务器地址