---
title: "Build an Express middleware to handle Cross-Origin Resource Sharing (CORS) for your MERN app."  
description: "Build an Express middleware to handle Cross-Origin Resource Sharing (CORS) for your MERN app."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159303/build-an-express-middleware-to-handle-cross-origin-resource-sharing-cors-for-your-mern-app  
category: "javascript"  
tags: ["javascript", "express js", "mern"]  
reading_time: 2 minutes  

---

# Build an Express middleware to handle Cross-Origin Resource Sharing (CORS) for your MERN app.

Build an [Express](https://www.mindstick.com/forum/156025/how-to-use-garmin-express-software) [middleware](https://www.mindstick.com/forum/159733/what-is-the-role-of-middleware-in-dot-net-core-web-api) to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) [Cross](https://www.mindstick.com/forum/33892/how-to-handle-cross-thread-exception-in-winforms)-[Origin](https://www.mindstick.com/forum/160458/access-control-allow-origin-in-asp-dot-net-core-6) [Resource Sharing](https://www.mindstick.com/forum/159747/what-is-the-purpose-of-cors-cross-origin-resource-sharing-in-a-dot-net-core-api-and-how-do-you-confi) (CORS) for your [MERN app](https://www.mindstick.com/forum/161430/how-do-you-handle-server-side-routing-with-express-in-a-mern-app).

## Replies

### Reply by Aryan Kumar

Sure, here is an example of an Express middleware to handle Cross-Origin [Resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) [Sharing](https://www.mindstick.com/forum/157603/write-about-time-sharing-system-and-real-time-operating-system) (CORS) for your [MERN](https://www.mindstick.com/forum/159452/fix-file-upload-failed-in-mern) app:

JavaScript

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

const app = express();

app.use(cors({
  origin: '*',
  optionsSuccessStatus: 200,
}));

app.get('/', (req, res) => {
  res.send('This is a CORS-enabled endpoint');
});

app.listen(3000, () => console.log('Server started on port 3000'));
```

This middleware first imports the `express` and `cors` modules. The `cors` module provides a middleware that can be used to enable CORS for Express applications.

The middleware then defines the `origin` and `optionsSuccessStatus` options for the `cors()` middleware. The `origin` option specifies the origins that are allowed to make cross-origin requests. The `optionsSuccessStatus` option specifies the status code that should be returned when the CORS preflight request is successful.

The middleware then uses the `use()` method to add the `cors()` middleware to the Express application.

Finally, the middleware defines a `get` handler for the `/` route. The `get` handler simply sends a message to the client indicating that the endpoint is CORS-enabled.

To run this middleware, you can save it as a file called `cors.js` and then run it from the command line:

```plaintext
node cors.js
```

This will start the Express server on port 3000. You can then test the endpoint by making a GET request to `http://localhost:3000/` from a browser or another application.


---

Original Source: https://www.mindstick.com/forum/159303/build-an-express-middleware-to-handle-cross-origin-resource-sharing-cors-for-your-mern-app

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
