Fireship · @fireship
CORS in 100 Seconds
Uploaded March 29, 2021 · Length 2:31
Summary
how to enable cors on a server to allow cross-origin requests
- cors
- cross-origin resource sharing
- express.js
- same origin policy
- access control allow origin header
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
cross-origin resource sharing or cores
- 0:03
is a mechanism that allows a website on
- 0:05
one url to request data from a different
- 0:07
url
- 0:07
and it's frustrated both front-end and
- 0:09
back-end developers since the dawn of
- 0:11
time you may have tried to use an image
- 0:13
on your website that lives on a
- 0:14
different url
- 0:15
only to end up with a broken image or
- 0:17
your website might try to fetch data
- 0:19
from your api
- 0:20
then fail with a coors error in the
- 0:21
console that happens
- 0:23
because the browser implements the same
- 0:25
origin policy as part of its security
- 0:27
model which allows a website to freely
- 0:29
request
- 0:30
images and data from its own url but
- 0:32
blocks anything from an external url
- 0:34
unless certain conditions are met when
- 0:36
the browser makes a request
- 0:37
it adds an origin header to the request
- 0:39
message if that request goes to a server
- 0:41
on the same origin
- 0:42
then it's allowed by the browser with no
- 0:44
questions asked however
- 0:45
if that request goes to a different url
- 0:47
then it's known as a cross origin
- 0:49
request
- 0:50
when sending the response the server
- 0:52
will add the access control allow origin
- 0:54
header
- 0:55
its value needs to match the origin
- 0:56
header on the request
- 0:58
or it can be a wild card which allows
- 0:59
any url to make the request
- 1:01
if it's a mismatch though the browser
- 1:03
will prevent the response data from
- 1:04
being shared with the client
- 1:05
this results in an error in the browser
- 1:07
but for security reasons there's very
- 1:09
limited information about what actually
- 1:11
went wrong
- 1:12
the solution to a corsair lies on the
- 1:14
server if you don't control the server
- 1:15
you're out of luck
- 1:16
if you do control it you likely just
- 1:18
need to configure it to respond with a
- 1:19
proper header in express.js for example
- 1:22
that can be achieved with a single line
- 1:23
of middleware code which tells the
- 1:25
server to include the course headers on
- 1:27
every response
- 1:28
now certain http requests like put or
- 1:31
any request with a non-standard header
- 1:32
will need to be preflighted
- 1:34
and just like at the airport it's a
- 1:36
sanity check that ensures the passenger
- 1:38
or request
- 1:38
is safe to fly on the airplane or server
- 1:41
the browser automatically knows when to
- 1:42
preflight and will first make a request
- 1:44
using the options http verb
- 1:46
the server will then respond saying yes
- 1:48
i allow this origin
- 1:49
to make requests with the following
- 1:51
methods at which point the main request
- 1:53
can happen
- 1:54
without fear of disaster that may sound
- 1:56
inefficient but the server can respond
- 1:57
with a max age header allowing the
- 1:59
browser to cache a preflight for a


