New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

express-cors-ts

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-cors-ts

CORS middleware for ExpressJS. Support typescript.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Table of contents

  • Table of contents
  • How to use

How to use

Example 1 - Easy way

import { expressCors } from 'express-cors-ts';
import express, { Request, Response } from 'express';


const app = express();

app.use(expressCors()); // allow all origins
// or 
app.use(expressCors({
    allowedOrigins: "*" // allow all origins
}));

Example 2

import { expressCors } from 'express-cors-ts';
import express, { Request, Response } from 'express';


const app = express();


app.use(expressCors({
    allowedOrigins: [
        'domain1.com', // Allow all from domain1.com with default configurations
        'domain2.com',
        '127.0.0.1', // Allow all from 127.0.0.1 with default configurations
        'localhost'
    ]
}));

Example 3

import { expressCors } from 'express-cors-ts';
import express, { Request, Response } from 'express';


const app = express();


app.use(expressCors({
    allowedOrigins: [
        'domain1.com', // Allow all with default configurations
        {
            hostnames: 'bookstore.com',
            allowMethods: 'GET',
        },
        {
            hostnames: 'admin-bookstore.com',
            ports: '*', // Allow all ports
            protocols: '*' // Allow all protocols
            allowMethods: '*', // Allow all methods
        },
        {
            hostnames: [ 'admin-my-blog.com', 'my-blog.com' ],
            ports: [ 80, 3000, 443, 8080 ],
            protocols: 'http',
            allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
            allowHeaders: [ 'Origin', 'X-Requested-With', 'Content-Type', 'Auth', 'Authorization' ],
            exposeHeaders: [ 'Token' ],
            allowCredentials: true,
        }
    ],
    otherOrigins: 'no-action' // Set action when origin does not exist in allowOrigins.
}));


Example 4

import { expressCors } from 'express-cors-ts';
import express, { Request, Response } from 'express';


const allows:AllowedOrigin[] = [
    'domain1.com',
    {
        hostnames: 'bookstore.com',
        allowMethods: 'GET',
    },
    {
        hostnames: 'admin-bookstore.com',
        allowMethods: '*',
        ports: '*',
        protocols: '*'
    },
];

const app = express();

app.use(expressCors({
    allowedOrigins: allows,
    
    otherOrigins: (req:Request, res:Response, next:NextFunction) => {
        // Your code here ...
        next();
    }
}));


Keywords

FAQs

Package last updated on 12 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc