Skip to content

Instantly share code, notes, and snippets.

@adjeim
Last active January 23, 2025 23:42
Show Gist options
  • Select an option

  • Save adjeim/a2ddb5214c92ce5d708fb0a3d6f073f6 to your computer and use it in GitHub Desktop.

Select an option

Save adjeim/a2ddb5214c92ce5d708fb0a3d6f073f6 to your computer and use it in GitHub Desktop.

Revisions

  1. adjeim renamed this gist Mar 31, 2021. 1 changed file with 0 additions and 0 deletions.
  2. adjeim renamed this gist Mar 31, 2021. 1 changed file with 0 additions and 0 deletions.
  3. adjeim renamed this gist Mar 31, 2021. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions README.md → CORS Cheat Sheet for Express + TypeScript.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    # CORS Cheat Sheet for Express + TypeScript

    1. Install `cors` package and its TypeScript types:
    ```bash
    npm install cors
  4. adjeim created this gist Mar 31, 2021.
    34 changes: 34 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # CORS Cheat Sheet for Express + TypeScript

    1. Install `cors` package and its TypeScript types:
    ```bash
    npm install cors
    npm install --save-dev @types/cors
    ```

    2. 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.
    ```javascript
    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');
    });
    ```