← All @molecule/* packages · App templates
@molecule/api-middleware-cookie-parserMiddleware · core · API (Node) · v1.0.1 · Apache-2.0
Cookie parser middleware interface
npm install @molecule/api-middleware-cookie-parser@molecule/api-middleware-cookie-parser is core middleware for the API, mounted once at startup.
import express from 'express'
import { cookieParser, createCookieParserMiddleware } from '@molecule/api-middleware-cookie-parser'
const app = express()
// Default parser (unsigned cookies) — requires the bond to be wired,
// e.g. @molecule/api-middleware-cookie-parser-express.
app.use(cookieParser)
// Signed cookies: pass the server-side secret; verified values land on
// req.signedCookies with the Express bond.
const sessionSecret = process.env.SESSION_SECRET ?? ''
app.use(createCookieParserMiddleware(sessionSecret))Providers (1): @molecule/api-middleware-cookie-parser-express
Works with: @molecule/api-bond
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Cookie parser middleware for molecule.dev.
Core interface — the actual implementation is provided via bonds.
Install a cookie parser bond (e.g., @molecule/api-middleware-cookie-parser-express)
to provide cookie parsing.
import express from 'express'
import { cookieParser, createCookieParserMiddleware } from '@molecule/api-middleware-cookie-parser'
const app = express()
// Default parser (unsigned cookies) — requires the bond to be wired,
// e.g. @molecule/api-middleware-cookie-parser-express.
app.use(cookieParser)
// Signed cookies: pass the server-side secret; verified values land on
// req.signedCookies with the Express bond.
const sessionSecret = process.env.SESSION_SECRET ?? ''
app.use(createCookieParserMiddleware(sessionSecret))
middleware
npm install @molecule/api-middleware-cookie-parser @molecule/api-bond
CookieParseOptionsOptions for cookie parsing.
interface CookieParseOptions {
decode?: (value: string) => string
}
CookieParserFactoryFactory function type for creating cookie parser middleware with secret and options.
type CookieParserFactory = (secret?: string | string[], options?: CookieParseOptions) => Middleware
MiddlewareGeneric middleware type — framework-agnostic.
type Middleware = (req: unknown, res: unknown, next: (err?: unknown) => void) => void
cookieParser(req, res, next)Default cookie parser middleware that delegates to the bonded implementation. Parses the Cookie header and populates req.cookies.
function cookieParser(req: unknown, res: unknown, next: (err?: unknown) => void): void
req — The incoming request object.res — The response object.next — The next middleware function.Returns: The result of the bonded cookie parser invocation.
createCookieParserMiddleware(secret, options)Creates cookie parser middleware with custom options via the bonded factory.
function createCookieParserMiddleware(
secret?: string | string[],
options?: CookieParseOptions,
): Middleware
secret — Secret string(s) for signed cookie verification.options — Cookie parsing options (e.g., custom decode function).Returns: A middleware function that parses cookies.
getCookieParser()Gets the bonded cookie parser middleware.
function getCookieParser(): Middleware
Returns: The bonded cookie parser middleware function.
getCookieParserFactory()Gets the bonded cookie parser factory.
function getCookieParserFactory(): CookieParserFactory | null
Returns: The factory function, or null if none has been bonded.
hasCookieParser()Checks if a cookie parser middleware has been bonded.
function hasCookieParser(): boolean
Returns: true if a cookie parser is available.
setCookieParser(parser)Bonds a cookie parser middleware implementation for use by getCookieParser() and cookieParser.
function setCookieParser(parser: Middleware): void
parser — The middleware function that parses cookies.setCookieParserFactory(factory)Bonds a cookie parser factory for creating parsers with custom secrets and options.
function setCookieParserFactory(factory: CookieParserFactory): void
factory — A function that creates cookie parser middleware from options.Peer dependencies:
@molecule/api-bond ^1.0.1@molecule/api-bondThis parses INCOMING cookies onto req.cookies. When your app SETS an auth/session cookie,
the flags are what matter:
httpOnly + secure + sameSite. httpOnly stops JS
(and therefore XSS) from reading the token; secure restricts it to HTTPS in production;
sameSite: 'lax'|'strict' blocks CSRF. A readable session cookie is stealable.SESSION_SECRET) when you need tamper detection, and keep the real state server-side
keyed by the session.