Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip already set headers on server initialization #345

Open
hpawe01 opened this issue Nov 17, 2017 · 3 comments
Open

Skip already set headers on server initialization #345

hpawe01 opened this issue Nov 17, 2017 · 3 comments

Comments

@hpawe01
Copy link

hpawe01 commented Nov 17, 2017

Hello,

I use the possibility to modify the Express server before starting it as described in https://github.com/holidayextras/jsonapi-server/blob/master/documentation/configuring.md#gaining-access-to-the-express-server

For example, I need cookie-authentication and have to add some headers:

 const app = jsonApi.getExpressServer();
 app.use(function(req, res, next) {
   res.set('Access-Control-Allow-Origin', 'http://localhost:4200');
   res.set('Access-Control-Allow-Credentials', 'true');
   return next();
 });

But the header Access-Control-Allow-Origin gets overwritten during the initialization of the json api server:

res.set({
'Content-Type': 'application/vnd.api+json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': req.headers['access-control-request-headers'] || '',
'Cache-Control': 'private, must-revalidate, max-age=0',
'Expires': 'Thu, 01 Jan 1970 00:00:00'
})

I changed it locally, so that the header is only set, when it is not already present:

if (!res.get('Access-Control-Allow-Origin')) {
  res.set('Access-Control-Allow-Origin', '*');
}

Should I prepare a pull request with this change? Or does this break something I am not aware of?

Cheers,
Friedrich

@paparomeo
Copy link
Contributor

It makes sense that already set headers are not overwritten. Please feel free to submit a PR, it would be appreciated.

@hpawe01
Copy link
Author

hpawe01 commented Nov 20, 2017

@paparomeo, I created a fork, implemented the check and added a test ( tests/custom-middleware.js): hpawe01@e6d3b77.

The single test succeeds (npm test -- --grep headers), but not together with the others (npm test). When I change the filename of the test to 0custom-middleware.js, the test is executed at first and it succeeds, but now the test options.js fails. It seems like all tests are using the same instance of the jsonApiTestServer, and that I cannot change the underlying express-server just for a single test. Do you have an idea how to achieve this?

@paparomeo
Copy link
Contributor

Thanks for the PR @hpawe01. I'm trying to solve some other more urgent issues, but I'll have a look at it as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants