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

Prevent Access-Control-Expose-Headers header to be overwritten #1409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

carlosanjos
Copy link

I tried using a middleware to create a new header to be consumed by a JavaScript web application, but I can't use the custom header because Access-Control-Expose-Headers won't allow it. So I decided to created the following middleware.

module.exports = (req, res, next) => {
    res.header('X-Hello', 'World');
    res.append('Access-Control-Expose-Headers', 'X-Hello');
    next();
}

But if I try the consume the API using using any pagination item, like _page Access-Control-Expose-Headers is overwritten by Access-Control-Expose-Headers: X-Total-Count, Link
I also tried to create a custom server, but I got the same result

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('./api/projects.json');
const middlewares = jsonServer.defaults();

server.use(middlewares);

server.use((req, res, next) => {
    res.setHeader(
        'X-Hello',
         'World'
    );

    res.setHeader(
        'Access-Control-Expose-Headers',
        'X-Hello',
    )

    next();
});


server.use(router);
server.listen(4000, () => {
    console.log('JSON Server is running');
});

This change will prevent the aforementioned header to be overwritten.

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

Successfully merging this pull request may close these issues.

1 participant