Socket
Socket
Sign inDemoInstall

corser

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

corser

A highly configurable, middleware compatible implementation of CORS.


Version published
Weekly downloads
2.4M
increased by1.7%
Maintainers
1
Weekly downloads
 
Created

What is corser?

Corser is a highly configurable, middleware-based Node.js package for handling Cross-Origin Resource Sharing (CORS) requests. It allows you to specify which origins, methods, headers, and other CORS-related settings are allowed for your server.

What are corser's main functionalities?

Basic CORS Setup

This code sets up a basic CORS configuration using Corser with an Express server. It allows all origins and methods by default.

const corser = require('corser');
const express = require('express');
const app = express();

app.use(corser.create());

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Custom CORS Configuration

This code demonstrates how to set up a custom CORS configuration with Corser. It restricts allowed origins to 'http://example.com', methods to 'GET' and 'POST', and adds a custom header 'X-Custom-Header'. It also supports credentials.

const corser = require('corser');
const express = require('express');
const app = express();

const corserOptions = {
  origins: ['http://example.com'],
  methods: ['GET', 'POST'],
  headers: corser.simpleRequestHeaders.concat(['X-Custom-Header']),
  supportsCredentials: true
};

app.use(corser.create(corserOptions));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Preflight Request Handling

This code shows how to handle preflight requests using Corser. It sets up the middleware to handle 'OPTIONS' requests, which are used by browsers to check CORS policy before sending the actual request.

const corser = require('corser');
const express = require('express');
const app = express();

const corserOptions = {
  methods: ['GET', 'POST', 'OPTIONS'],
  headers: corser.simpleRequestHeaders.concat(['X-Custom-Header']),
  supportsCredentials: true
};

app.use(corser.create(corserOptions));

app.options('*', corser.create(corserOptions));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Other packages similar to corser

Keywords

FAQs

Package last updated on 16 Aug 2016

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