This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Allow CORS in Node.js and Express

    Joel LordJoel Lord

    In this quick lesson, we see how to add the CORS middleware so that our Express server can handle requests from different origins.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 If you have an API that runs on a different server or even a different port than your application, and you try to do a fetch, you should see a message in your console complaining about Access-Control-Allow-Origin.

    00:19 The fix for this in your Express server is very simple. First of all, you need to require the CORS library. You'll also need to install it using npm install cors. Secondly, you need to tell your Express server to use this middleware by using app.use and the library that we've just installed.

    00:46 Now, if you restart your server and try the same fetch call in your browser, you won't have that CORS error any more.

    Discuss

    Discuss