简体   繁体   中英

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:
<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. That call must be listened in 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. 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. Are you sure it's everything set up correctly on your server?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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