Kepin

home blog

corsetta

Posted on: 23/03/2022
Last edit: 23/03/2022
cover images
CORS in many different programming language

Yes, another rosetta but for cors. Pardon me if some example goes unexpected. Please open issue or create pull request if some example is wrong.

Nodejs

express

This example also work with polka, and any express-like lib.

const express = require("express");
const cors = require("cors");
const app = express();
app.use(cors());
const express = require("express");
const cors = require("cors");
const app = express();
app.use(cors());

koa

const Koa = require("koa");
const cors = require("@koa/cors");
const app = new Koa();
app.use(cors());
const Koa = require("koa");
const cors = require("@koa/cors");
const app = new Koa();
app.use(cors());

fastify

const fastify = require("fastify")();
fastify.register(require("fastify-cors"), { origin: true });
const fastify = require("fastify")();
fastify.register(require("fastify-cors"), { origin: true });

restify

const restify = require("restify");
const server = restify.createServer();
server.use(restify.CORS());
const restify = require("restify");
const server = restify.createServer();
server.use(restify.CORS());

Deno

oak

import { Application } from "https://deno.land/x/oak@v10.4.0/mod.ts";
const app = new Application();
app.use((ctx, next) => {
	ctx.response.headers.set("access-control-allow-origin", "*");
	return next();
});
import { Application } from "https://deno.land/x/oak@v10.4.0/mod.ts";
const app = new Application();
app.use((ctx, next) => {
	ctx.response.headers.set("access-control-allow-origin", "*");
	return next();
});

Golang

chi

package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/cors"
)

func main() {
	r := chi.NewRouter()
	r.Use(cors.Handler(cors.Options{
		AllowedOrigins: []string{"*"},
	}))
	http.ListenAndServe(":8080", r)
}
package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/cors"
)

func main() {
	r := chi.NewRouter()
	r.Use(cors.Handler(cors.Options{
		AllowedOrigins: []string{"*"},
	}))
	http.ListenAndServe(":8080", r)
}

std/http

package main

import (
	"net/http"

	"github.com/rs/cors"
)

func main() {
	mux := http.NewServerMux()
	http.ListenAndServe(":8080", cors.Default().Handler(mux))
}
package main

import (
	"net/http"

	"github.com/rs/cors"
)

func main() {
	mux := http.NewServerMux()
	http.ListenAndServe(":8080", cors.Default().Handler(mux))
}

fiber

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
)

func main() {
	app := fiber.New()
	app.Use(cors.New())
}
package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
)

func main() {
	app := fiber.New()
	app.Use(cors.New())
}

gin

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-contrib/cors"
)

func main() {
	r := gin.Default()
	r.Use(cors.Default())
}
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-contrib/cors"
)

func main() {
	r := gin.Default()
	r.Use(cors.Default())
}
My Small Website
© 2022 Kepin