NOTICE: 2021/04/09 servestjs.org no longer hosts typescript files. Use https://deno.land/x/servest

Handle CORS

corsis built-in middleware for CORS (cross-origin resource sharing).

import { createApp, createRouter, serveStatic } from "https://deno.land/x/[email protected]/mod.ts";
import { cors } from "https://deno.land/x/[email protected]/middleware/cors.ts";
const app = createApp();
app.use(
  // allow access to static resource only from (*.)servestjs.org
  cors({
    origin: /servestjs\.org$/,
    methods: ["GET", "HEAD"],
  }),
  serveStatic("public"),
);
const api = createRouter();
// allow all access to /api routes
api.use(cors({
  origin: "*",
  methods: ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"],
  allowedHeaders: ["x-my-api-token"],
  maxAge: 300,
}));
app.route("/api", api);
app.listen({ port: 8899 });