Express CORS Works On Others Endpoints But One
I have a simple Express project as my API endpoint using cors as middleware.
The cors works on any others endpoint but one. Here is my code snapshot:
const express = require('express');
var cors = require('cors');
const app = express();
app.use(cors());
app.get('/shuttles',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
app.get('/deposit',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
The CORS in /deposit
is working but not with /shuttles
.
NB: Nevermind the tokenPassport
require. It is for authorization.
EDIT
Here is the snapshot of network tab in chrome devtool. It's only send OPTIONS
request and that is the response header. No Access-Control-Allow-Origin
header in the response.
node.js express cors
add a comment |
I have a simple Express project as my API endpoint using cors as middleware.
The cors works on any others endpoint but one. Here is my code snapshot:
const express = require('express');
var cors = require('cors');
const app = express();
app.use(cors());
app.get('/shuttles',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
app.get('/deposit',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
The CORS in /deposit
is working but not with /shuttles
.
NB: Nevermind the tokenPassport
require. It is for authorization.
EDIT
Here is the snapshot of network tab in chrome devtool. It's only send OPTIONS
request and that is the response header. No Access-Control-Allow-Origin
header in the response.
node.js express cors
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40
add a comment |
I have a simple Express project as my API endpoint using cors as middleware.
The cors works on any others endpoint but one. Here is my code snapshot:
const express = require('express');
var cors = require('cors');
const app = express();
app.use(cors());
app.get('/shuttles',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
app.get('/deposit',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
The CORS in /deposit
is working but not with /shuttles
.
NB: Nevermind the tokenPassport
require. It is for authorization.
EDIT
Here is the snapshot of network tab in chrome devtool. It's only send OPTIONS
request and that is the response header. No Access-Control-Allow-Origin
header in the response.
node.js express cors
I have a simple Express project as my API endpoint using cors as middleware.
The cors works on any others endpoint but one. Here is my code snapshot:
const express = require('express');
var cors = require('cors');
const app = express();
app.use(cors());
app.get('/shuttles',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
app.get('/deposit',
tokenPassport.authenticate('bearer', { session: false }),
(req, res) => {
// ....
// implementation goes here...
// ....
})
The CORS in /deposit
is working but not with /shuttles
.
NB: Nevermind the tokenPassport
require. It is for authorization.
EDIT
Here is the snapshot of network tab in chrome devtool. It's only send OPTIONS
request and that is the response header. No Access-Control-Allow-Origin
header in the response.
node.js express cors
node.js express cors
edited Nov 27 '18 at 5:28
Kevin Christian
asked Nov 26 '18 at 5:17
Kevin ChristianKevin Christian
366
366
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40
add a comment |
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40
add a comment |
3 Answers
3
active
oldest
votes
you can create your own middleware, in your app.js
or server.js
file.
//CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept,Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
if you want to use the cros
module
const corsOptions = {
"origin": "*",
"methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
// other options
}
app.use(cors(corsOptions));
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work onshuttles
endpoint but works on other endpoints.
– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly addingcors
this route. something like thisapp.get('/shuttles', cors(corsOptions), other parameters
.
– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing ofcors
.
– sanprodev
Nov 28 '18 at 5:16
add a comment |
Hi can you try adding this line in the express server
. also can you check whether from the client application when you are invoking the
app.options('*', cors());/shuttles
end point are you adding 'access-control-origins' header to *
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
add a comment |
After some meditation finding the solution in the universe, my Express application is hosted in Google App Engine and I realized that my dispatch.yaml
contains
- url: "*/shuttles*"
service: shuttle
It is the source of the problem.
Just rework the shuttle service and the routing then it solved :D
Thanks for all your comment, they all right.
add a comment |
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%2f53475102%2fexpress-cors-works-on-others-endpoints-but-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can create your own middleware, in your app.js
or server.js
file.
//CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept,Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
if you want to use the cros
module
const corsOptions = {
"origin": "*",
"methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
// other options
}
app.use(cors(corsOptions));
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work onshuttles
endpoint but works on other endpoints.
– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly addingcors
this route. something like thisapp.get('/shuttles', cors(corsOptions), other parameters
.
– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing ofcors
.
– sanprodev
Nov 28 '18 at 5:16
add a comment |
you can create your own middleware, in your app.js
or server.js
file.
//CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept,Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
if you want to use the cros
module
const corsOptions = {
"origin": "*",
"methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
// other options
}
app.use(cors(corsOptions));
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work onshuttles
endpoint but works on other endpoints.
– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly addingcors
this route. something like thisapp.get('/shuttles', cors(corsOptions), other parameters
.
– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing ofcors
.
– sanprodev
Nov 28 '18 at 5:16
add a comment |
you can create your own middleware, in your app.js
or server.js
file.
//CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept,Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
if you want to use the cros
module
const corsOptions = {
"origin": "*",
"methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
// other options
}
app.use(cors(corsOptions));
you can create your own middleware, in your app.js
or server.js
file.
//CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept,Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
if you want to use the cros
module
const corsOptions = {
"origin": "*",
"methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
// other options
}
app.use(cors(corsOptions));
answered Nov 26 '18 at 6:07
sanprodevsanprodev
808
808
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work onshuttles
endpoint but works on other endpoints.
– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly addingcors
this route. something like thisapp.get('/shuttles', cors(corsOptions), other parameters
.
– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing ofcors
.
– sanprodev
Nov 28 '18 at 5:16
add a comment |
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work onshuttles
endpoint but works on other endpoints.
– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly addingcors
this route. something like thisapp.get('/shuttles', cors(corsOptions), other parameters
.
– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing ofcors
.
– sanprodev
Nov 28 '18 at 5:16
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work on
shuttles
endpoint but works on other endpoints.– Kevin Christian
Nov 27 '18 at 5:25
thank you for your suggestion but unfortunately it doesn't work :( the cors still doesn't work on
shuttles
endpoint but works on other endpoints.– Kevin Christian
Nov 27 '18 at 5:25
I'm not sure why it's not working for that particular route, can you try explicitly adding
cors
this route. something like this app.get('/shuttles', cors(corsOptions), other parameters
.– sanprodev
Nov 27 '18 at 9:52
I'm not sure why it's not working for that particular route, can you try explicitly adding
cors
this route. something like this app.get('/shuttles', cors(corsOptions), other parameters
.– sanprodev
Nov 27 '18 at 9:52
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
Have you see my edit on the question? I attach a snapshot of chrome devtool, any clue from that?
– Kevin Christian
Nov 28 '18 at 5:13
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing of
cors
.– sanprodev
Nov 28 '18 at 5:16
I can only see that the header is not there, you will have to do some debugging on your end, have you tried explicit passing of
cors
.– sanprodev
Nov 28 '18 at 5:16
add a comment |
Hi can you try adding this line in the express server
. also can you check whether from the client application when you are invoking the
app.options('*', cors());/shuttles
end point are you adding 'access-control-origins' header to *
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
add a comment |
Hi can you try adding this line in the express server
. also can you check whether from the client application when you are invoking the
app.options('*', cors());/shuttles
end point are you adding 'access-control-origins' header to *
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
add a comment |
Hi can you try adding this line in the express server
. also can you check whether from the client application when you are invoking the
app.options('*', cors());/shuttles
end point are you adding 'access-control-origins' header to *
Hi can you try adding this line in the express server
. also can you check whether from the client application when you are invoking the
app.options('*', cors());/shuttles
end point are you adding 'access-control-origins' header to *
answered Nov 26 '18 at 5:25
Vaibhav Kumar GoyalVaibhav Kumar Goyal
664613
664613
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
add a comment |
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
thank you for your reply, but unfortunately it doesn't work on any endpoints :(
– Kevin Christian
Nov 27 '18 at 5:29
add a comment |
After some meditation finding the solution in the universe, my Express application is hosted in Google App Engine and I realized that my dispatch.yaml
contains
- url: "*/shuttles*"
service: shuttle
It is the source of the problem.
Just rework the shuttle service and the routing then it solved :D
Thanks for all your comment, they all right.
add a comment |
After some meditation finding the solution in the universe, my Express application is hosted in Google App Engine and I realized that my dispatch.yaml
contains
- url: "*/shuttles*"
service: shuttle
It is the source of the problem.
Just rework the shuttle service and the routing then it solved :D
Thanks for all your comment, they all right.
add a comment |
After some meditation finding the solution in the universe, my Express application is hosted in Google App Engine and I realized that my dispatch.yaml
contains
- url: "*/shuttles*"
service: shuttle
It is the source of the problem.
Just rework the shuttle service and the routing then it solved :D
Thanks for all your comment, they all right.
After some meditation finding the solution in the universe, my Express application is hosted in Google App Engine and I realized that my dispatch.yaml
contains
- url: "*/shuttles*"
service: shuttle
It is the source of the problem.
Just rework the shuttle service and the routing then it solved :D
Thanks for all your comment, they all right.
answered Dec 4 '18 at 7:23
Kevin ChristianKevin Christian
366
366
add a comment |
add a comment |
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%2f53475102%2fexpress-cors-works-on-others-endpoints-but-one%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
Use stackoverflow.com/posts/53475102/edit to edit/update your question and add the exact error message that your browser is logging in the devtools console when you make a request to the '/shuttles' endpoint. Also, add a code snippet showing the frontend JavaScript code you’re using to make the request.
– sideshowbarker
Nov 27 '18 at 3:40