Skip to content

How to use withCors middleware #16

Answered by georgeboot
georgeboot asked this question in Q&A
Discussion options

You must be logged in to vote

How I solved it:

// corshelper.js
export const handleCors = (options = {}) => request => {
    const {
        origin = '*',
        methods = 'GET, POST, PATCH, DELETE',
        headers = 'referer, origin, content-type',
        maxAge = null,
        allowCredentials = false,
    } = options

    if (
        request.headers.get('Origin') !== null &&
        request.headers.get('Access-Control-Request-Method') !== null
    ) {
        const corsHeaders = {
            'Access-Control-Allow-Origin': origin,
            'Access-Control-Allow-Methods': methods,
            'Access-Control-Allow-Headers': headers,
        }

        if (allowCredentials) {
            corsHeaders['Access-Co…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ojknation
Comment options

@towfiqi
Comment options

Answer selected by georgeboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants