Javascript Node.js Axios用于本地服务器端脚本调用

Javascript Node.js Axios用于本地服务器端脚本调用,javascript,node.js,ajax,axios,Javascript,Node.js,Ajax,Axios,我有一个Node.js应用程序运行在端口3000上,它使用axios进行服务器端ajax调用 它的工作如下 我的axio ajax调用是在/public/views/example.js中进行的 example() { axios.get ( // server ip, port and route "http://192.168.1.5:3000/example", { params : { ar

我有一个Node.js应用程序运行在端口3000上,它使用axios进行服务器端ajax调用

它的工作如下

我的axio ajax调用是在/public/views/example.js中进行的

example() {

    axios.get (
        // server ip, port and route
        "http://192.168.1.5:3000/example", {
            params : {
                arg01: "nothing"
            }
        }
    )
    .then (
        result => console.log(result)
    )
    .catch (
        error => console.log(error)
    );

}
router.get("/example", function(req, res) {

    // just to test the ajax request and response

    var result = req.query.arg01;
    res.send(result);

});
以及它正在调用的路由/public/logic/example\u route.js

example() {

    axios.get (
        // server ip, port and route
        "http://192.168.1.5:3000/example", {
            params : {
                arg01: "nothing"
            }
        }
    )
    .then (
        result => console.log(result)
    )
    .catch (
        error => console.log(error)
    );

}
router.get("/example", function(req, res) {

    // just to test the ajax request and response

    var result = req.query.arg01;
    res.send(result);

});
因此,当我从网络内部运行它时,这一切工作正常,但如果我尝试从网络外部运行它(使用转发了3000个端口的DNS),它会失败,我认为这是因为当在外部执行时,192.168.1.5不再有效,因为我必须使用DNS

当我将axios调用更改为以下内容时

example() {

    axios.get (
        // server ip, port and route
        "http://www.dnsname.com:3000/example", {
            params : {
                arg01: "nothing"
            }
        }
    )
    .then (
        result => console.log(result)
    )
    .catch (
        error => console.log(error)
    );

}
然后,它会在外部工作,但不会在内部工作。有办法解决这个问题吗

我知道,当使用php进行ajax调用时,我没有这个问题,因为我可以使用脚本的实际位置而不是路由

$.ajax({
    url      : "logic/example.php",
    type     : "GET",
    dataType : "json",
    data     : {
                  "arg01":"nothing"
               },
    success  : function(result) {
                  console.log(result);
               },
    error    : function(log) {
                  console.log(log.message);
               }
});

Node.js和axios有可能实现类似的功能吗?

您可以在不使用实际主机和端口的情况下使用路径

example() {    
    axios.get (
        // just the path without host or port
        "/example", {
            params : {
                arg01: "nothing"
            }
        }
    )
    .then (
        result => console.log(result)
    )
    .catch (
        error => console.log(error)
    );    
}

您是否尝试将
/example
用作url而不是
http://www.dnsname.com:3000/example
?哇,真是太棒了,我不知道它这么聪明,你能给我一个答案吗?这样我就可以接受了。他补充道。谢谢谢谢,我没想到这个解决方案会这么好