How to allow CORS request with Node Express

Published: March 31, 2019

If you are serving your own Node Express server and calling your API from JavaScript, you might get this error.

Cors error

This happens because of security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.

There is an easy fix for this. Open your project in the terminal.

npm i cors –save

Then add this to your node JavaScript file.

const cors = require('cors')

This has enabled CORS for all requests on your node express server. You can now refresh your client-side page.