- Install
cors
package and its TypeScript types:
npm install cors
npm install --save-dev @types/cors
- Update the entry point of your Express app to allow your server to use
cors
middleware.
Configure your CORS options with the origins you would like to allow.
import express from 'express';
import cors from 'cors';
const app = express();
// Add a list of allowed origins.
// If you have more origins you would like to add, you can add them to the array below.
const allowedOrigins = ['http://localhost:3000'];
const options: cors.CorsOptions = {
origin: allowedOrigins
};
// Then pass these options to cors:
app.use(cors(options));
app.use(express.json());
app.listen(5000, () => {
console.log('Express server listening on port 5000');
});
perfect! thankyou