简体   繁体   English

如何从index.html对dockerized express服务器进行http调用?

[英]How to make a http call to dockerized express server from index.html?

I don't understand how I can make http call from index.html to express server which serves that index.html and is located inside docker container. 我不明白如何从index.html进行http调用,以表示服务于该index.html的服务器,该服务器位于docker容器内。

index.html:
<script type="text/javascript">
      var getAppId = new XMLHttpRequest();
      getAppId.open("GET", "/appId", false);
      getAppId.send(null);
</script>

I need that http call to get know the app id which is presented by docker env variable. 我需要该http调用来了解docker env变量提供的应用程序ID。 That call must be listened in server.js (express.js) : 该调用必须在server.js(express.js)中进行监听:

router.get("/appId",(req,res) => {
  res.send({applicationId : process.env.APP_ID});
});

But when I run my dockerized app I see that my http call receives 404 http code. 但是,当我运行dockerized应用程序时,我看到我的http呼叫收到404 http代码。 Please help! 请帮忙!

I would use fetch: 我会使用提取:

fetch('www.example.com/appId', {
    method: 'GET',
    credentials: 'include',
    headers: { "Content-Type": "application/json"}
}).then(function(res) {
    return res.json();
    }).then(function(res){
        if(res){
           doSomesthing();
        }
        else{
            console.log('yourError')
        }
    }).catch((e)=>{console.log(e)})

But 404 means the route is not found. 但是404表示找不到路由。 Are you sure it's everything set up correctly on your server? 您确定服务器上的所有设置都正确吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM