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
const cors = require('cors');
- This imports the cors package.app.use(cors());
- This uses the cors middleware to disable CORS.
Helpful links
More of Expressjs
- How can I use Zipkin to trace requests in Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I use an ExpressJS webhook to receive data from an external source?
- How can I use express-zip js to zip and download files?
- How do I set up a YAML configuration file for a Node.js Express application?
- How do I implement CSRF protection in an Express.js application?
- How can I use Express.js and Keycloak together to secure an application?
- How do I set the time zone in Express.js?
- How can I use Express.js to answer questions?
- How can I use the x-forwarded-for header in Express.js?
See more codes...