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!
javascript node.js docker express web
add a comment |
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!
javascript node.js docker express web
add a comment |
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!
javascript node.js docker express web
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!
javascript node.js docker express web
javascript node.js docker express web
asked Nov 13 '18 at 14:42
Self_taughtSelf_taught
176
176
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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?
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283483%2fhow-to-make-a-http-call-to-dockerized-express-server-from-index-html%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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?
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
|
show 5 more comments
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?
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
|
show 5 more comments
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?
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?
edited Nov 13 '18 at 15:06
answered Nov 13 '18 at 14:55
charly1212charly1212
13518
13518
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
|
show 5 more comments
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
Yes, I am totally sure that everything set up correctly. I think problem is that docker app has domen name e.g. test.com and behind this domen name express exists. And problem that index.html make a call to domen name but not to express. Don't know how to redirect call directly to express
– Self_taught
Nov 13 '18 at 15:00
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
and in fetch you also you have to write the full route including domain www.example.com/appId
– charly1212
Nov 13 '18 at 15:06
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
One more thing, make sure your docker container listens to port 80.
– charly1212
Nov 13 '18 at 15:14
1
1
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
@Self_taught. How should "/appId" get transformed automatically to "exmaple.com/appId" ? "/appId" is a const string and will not get converted. And if your docker exposes port 8085 it can not get your requests, because the browser automatically requests on port 80. You have to map port 80 of express to port 80 on your host. And if you're using ssl you probably wanna setup a reverse proxy server before your docker container listening on port 443. In this case you can leave your docker port as it is, but make sure forwarding works from 443-->8050->express port (e.g. 80)
– charly1212
Nov 13 '18 at 22:17
1
1
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
you can also work without domains but only with ip, that won't work with ssl though. if server and client is on the same machine you can also work with "localhost/appId" instead of domain name or ip. either you use the "ip/appId" or "domain/appId" or "localhost/appId"
– charly1212
Nov 14 '18 at 14:09
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283483%2fhow-to-make-a-http-call-to-dockerized-express-server-from-index-html%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown