🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

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.

2.0.1
latest
Source
npm
Version published
Weekly downloads
3.6M
2.72%
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

cors

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