Cookie parsing
Given the cookie name, get the value of a cookie. You can also use cookies for A/B testing.
import { parse } from "cookie";export default {async fetch(request) {// The name of the cookieconst COOKIE_NAME = "__uid";const cookie = parse(request.headers.get("Cookie") || "");if (cookie[COOKIE_NAME] != null) {// Respond with the cookie valuereturn new Response(cookie[COOKIE_NAME]);}return new Response("No cookie with name: " + COOKIE_NAME);},};
const handler: ExportedHandler = {async fetch(request) {// The name of the cookieconst COOKIE_NAME = "__uid";const cookie = parse(request.headers.get("Cookie") || "");if (cookie[COOKIE_NAME] != null) {// Respond with the cookie valuereturn new Response(cookie[COOKIE_NAME]);}return new Response("No cookie with name: " + COOKIE_NAME);},};export default handler;