Skip to main content

Recipes

Attaching Express middleware

import { ExpressMiddlewareUtils } from '@quik/http-express';
import { Decorators, QRoute } from '@quik/http';

@ExpressMiddlewareUtils.RouteMiddleware((req, _res, next) => next())
@Decorators.Route.Route('/health')
export class HealthRoute extends QRoute {
@ExpressMiddlewareUtils.EndpointMiddleware((req, _res, next) => next())
@Decorators.Endpoint.GET('/')
async get() {
return { status: 'ok' };
}
}

RouteMiddleware applies to every endpoint on the route; EndpointMiddleware applies to a single endpoint.

Reading and writing cookies

import { Decorators, QRoute, type IQEvent } from '@quik/http';

@Decorators.Route.Route('/preferences')
export class PreferencesRoute extends QRoute {
@Decorators.Endpoint.POST('/theme')
async saveTheme(event: IQEvent) {
event.setCookie('theme', 'dark', {
maxAge: 30 * 24 * 60 * 60 * 1000,
sameSite: 'lax'
});

return { saved: true };
}

@Decorators.Endpoint.DELETE('/theme')
async clearTheme(event: IQEvent) {
event.clearCookie('theme');

return { cleared: true };
}
}

QExpressContext applies httpOnly: true, secure: http.cookie.secure, and domain: http.cookie.domain as defaults; options passed to setCookie/clearCookie override those defaults.

Plugging in a production session store

import RedisStore from 'connect-redis';
import { createClient } from 'redis';
import { QExpressEngine } from '@quik/http-express';

const client = createClient();
await client.connect();
QExpressEngine.setSessionStore(new RedisStore({ client }));

Call setSessionStore before the HTTP module boots. Alternatively, set http.session.store to file or redis and let @quik/http-express's own setup() create the store from createFileSessionStore()/createRedisSessionStore().

Serving a single-page app

{
http: {
static: {
serve: true,
global: true,
folder: './public',
path: '',
singlePageApplication: {
enable: true,
indexFile: 'index.html'
}
}
}
}

Requests under http.paths.api and http.paths.docs are excluded from the SPA fallback rewrite.

Extending the Scalar API reference configuration

import { ScalarHooks } from '@quik/http-express';

ScalarHooks.addConfiguration((config) => ({ ...config, theme: 'purple' }));

Registered hooks run when the OpenAPI docs route builds the Scalar configuration; onReset() clears them on a full bootstrap restart.