Skip to content

Instantly share code, notes, and snippets.

@didyhu didyhu/allow-cors.md
Last active May 22, 2019

Embed
What would you like to do?

Allow CORS

server

// listens foo.com:1234
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT")
    next()
})

client

// load in page other than foo.com:1234/*
fetch("http://foo.com:1234/get", {
    mode: "cors"
}).then(res => res.text()).then(text => {
    console.log(text)
})

fetch("http://foo.com:1234/post", {
    mode: "cors",
    method: "post"
}).then(res => res.json()).then(text => {
    console.log(text)
})

fetch("http://foo.com:1234/put", {
    mode: "cors",
    method: "put"
}).then(res => res.json()).then(text => {
    console.log(text)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.