# Enabling CORS for Express-GraphQL & Apollo Server | Prisma
URL: https://www.prisma.io/blog/enabling-cors-for-express-graphql-apollo-server-1ef999bfb38d
Published: 2017-11-22
Authors: Nikolas Burk
Description: Learn how to enable CORS in Express-GraphQL and Apollo Server so browser clients can safely call your GraphQL API across origins.
> Blog posts describe Prisma as it was on the date shown and are not kept current. Before implementing from a post, check the documentation index at https://www.prisma.io/docs/llms.txt and the changelog at https://www.prisma.io/changelog.md.
> For an index of every published post, fetch https://www.prisma.io/blog/llms.txt.
Learn how to configure cross-origin resource sharing on your express-based GraphQL servers.
## What is CORS? [#what-is-cors]
*Cross-origin resource sharing*, short [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing), is a protection mechanism for web pages, allowing the browser to safely load resources from domains that are different from the one it originally loaded the page from.
As an example, say you’re accessing `https://www.prisma.io/` in your browser. The browser downloads the HTML and JavaScript for the site in order to render it for you. Now, some data to be displayed on the site actually is stored somewhere else, on a different server — a *different origin*.
> Two URLs are said to have the same origin if the following three properties are identical for them: domain, protocol and port:
> `http://localhost:3000`, `ws://localhost:3000` and `http://localhost:4000` all have **different origins**.
> `http://localhost:3000` and `http://localhost:3000/graphql` have the **same origin**.
In order for your browser to load the data from that other server, the other server needs to set `Access-Control` headers properly in order to determine its policy regarding cross-origin resource access. For example, by simply specifying `Access-Control-Allow-Origin: *`, the server indicates to the browser that it will allow CORS anywhere.
## How about an example [#how-about-an-example]
Now, imagine the following scenario. You’re starting out with a new project and for now are only developing locally on your machine. You used [`create-react-app`](https://github.com/facebookincubator/create-react-app) to bootstrap your frontend and for the backend you setup a simple [express.js](https://github.com/expressjs) server (either based on [`express-graphql`](https://github.com/graphql/express-graphql), [`graphql-yoga`](https://github.com/prismagraphql/graphql-yoga) or [`apollo-server`](https://github.com/apollographql/apollo-server)).
In fact, we prepared an [example](https://github.com/nikolasburk/graphql-cors-example) that mimics this exact scenario:
### Server [#server]
```js
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphql-tools')
const typeDefs = `
type Query {
hello(name: String): String!
}
`
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
},
}
const myGraphQLSchema = makeExecutableSchema({ typeDefs, resolvers })
const PORT = 4000
const app = express()
// app.use(cors()) // not having cors enabled will cause an access control error
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }))
app.get('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
console.log(`Server listening on http://localhost:${PORT} ...`)
app.listen(PORT)
```
> Notice that line 23 is commented out — CORS is not enabled!
### Frontend [#frontend]
```js
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
import registerServiceWorker from './registerServiceWorker'
import { ApolloProvider } from 'react-apollo'
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-client-preset'
const httpLink = new HttpLink({ uri: 'http://localhost:4000/graphql' })
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
})
ReactDOM.render(