Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/cors

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/cors

Fastify CORS

  • 10.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
821K
increased by5.71%
Maintainers
0
Weekly downloads
 
Created

What is @fastify/cors?

@fastify/cors is a Fastify plugin that enables Cross-Origin Resource Sharing (CORS) for your Fastify application. It allows you to configure how your server handles cross-origin requests, which is essential for enabling web applications to interact with resources from different origins.

What are @fastify/cors's main functionalities?

Enable CORS with default settings

This code demonstrates how to enable CORS with the default settings in a Fastify application. By registering the @fastify/cors plugin without any options, it allows all cross-origin requests.

const fastify = require('fastify')();
const cors = require('@fastify/cors');

fastify.register(cors);

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Enable CORS with specific origin

This code demonstrates how to enable CORS for a specific origin. By passing an options object with the 'origin' property set to 'https://example.com', only requests from this origin will be allowed.

const fastify = require('fastify')();
const cors = require('@fastify/cors');

fastify.register(cors, { origin: 'https://example.com' });

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Enable CORS with multiple origins

This code demonstrates how to enable CORS for multiple origins. By passing an array of origins to the 'origin' property, requests from any of these origins will be allowed.

const fastify = require('fastify')();
const cors = require('@fastify/cors');

fastify.register(cors, { origin: ['https://example.com', 'https://another-example.com'] });

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Enable CORS with custom headers

This code demonstrates how to enable CORS with custom headers. By passing an options object with the 'allowedHeaders' property, you can specify which headers are allowed in cross-origin requests.

const fastify = require('fastify')();
const cors = require('@fastify/cors');

fastify.register(cors, { allowedHeaders: ['Content-Type', 'Authorization'] });

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/cors

Keywords

FAQs

Package last updated on 22 Sep 2024

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