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

Axios ignore cors from express node js #265

Closed
KjshServer opened this issue Mar 12, 2022 · 3 comments
Closed

Axios ignore cors from express node js #265

KjshServer opened this issue Mar 12, 2022 · 3 comments
Labels

Comments

@KjshServer
Copy link

KjshServer commented Mar 12, 2022

I have a backend and a frontend in node js and I want to decide which domain can do get requests, post requests, etc, from axios or fetch.

From the backend with the cors module I configure the origin with the domain that will have the permission to send requests.

If from the frontend from an index.html I use axios or fetch if it respects the cors, but if I use axios or fetch from the node js server with express it ignores cors and lets see the request.

Example:

main.js Backend

import express from "express"
import cors from "cors"

const app = express()

const allowedOrigins = ["http://www.frontend1.com", "http://frontend2.com", "http://localhost:3500"]

const credentials = (req, res, next) => {
    const origin = req.headers.origin
    if (allowedOrigins.includes(origin)) {
        res.header("Access-Control-Allow-Credentials", true)
    }
    next()
}

const corsOptions = {
    origin: (origin, callback) => {
        if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
            callback(null, true)
        } else {
            callback(new Error("Not allowed by CORS"))
        }
    },
    optionsSuccessStatus: 200
}

app.use(credentials)

app.use(cors(corsOptions))

app.get("/", (req, res) => {
    res.json({
        status: "success",
        message: "Backend"
    })
})

app.listen(4000, () => {
    console.log(`Backend is running on port 4000 http://127.0.0.1:4000`)
})

main.js Frontend

import express from "express"
import axios from "axios"

const app = express()

app.get("/", (req, res) => {
    axios
        .get("http://127.0.0.1:4000")
        .then(response => {
            res.json(response.data)
        })
        .catch(error => {
            res.json(error)
        })
})

app.listen(3000, () => {
    console.log(`Frontend is running on port 3000 http://127.0.0.1:3000`)
})

This should give a cors error, but no, it shows the json at the end and that's what I don't want.
I hope you can help me, thanks in advance :(

@dougwilson
Copy link
Contributor

This is an issue with axios.

@KjshServer
Copy link
Author

This is an issue with axios.

For me it would be a vulnerability on the part of cors since it is on the backend side, sorry if I'm wrong.

@dougwilson
Copy link
Contributor

Hi, sorry you are unfamiliar with how CORS functions. All controls are performed on the client side in CORS. You can find the specs and exactly how it works described in many places, including on MDN. The CORS spec is simply a mechanism to set headers on the response to control how the client side adheres same-origin restrictions.

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

No branches or pull requests

2 participants