9951 explained code solutions for 126 technologies


expressjsHow do I disable CORS in Express.js?


To disable CORS in Express.js, you can use the cors middleware package. To use it, you will need to install it first:

$ npm install cors

Then, you can use it in your Express.js app:

const express = require('express');
const cors = require('cors');

const app = express();

// Disable CORS
app.use(cors());

// Your routes
app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000);

The code above will disable CORS in your Express.js app.

Code explanation

  1. const cors = require('cors'); - This imports the cors package.
  2. app.use(cors()); - This uses the cors middleware to disable CORS.

Helpful links

Edit this code on GitHub