{
  "id": "a09434b760fe98889f5a37cc",
  "source": "solid:signals",
  "type": "github-document",
  "title": "create-middleware",
  "content": "---\ntitle: createMiddleware\nuse_cases: \u003e-\n  request interception, logging, authentication checks, cors setup, response\n  modification, api middleware\ntags:\n  - middleware\n  - request\n  - response\n  - lifecycle\n  - interceptor\n  - hooks\nversion: '1.0'\ndescription: \u003e-\n  Add middleware to SolidStart request lifecycle. Intercept requests and\n  responses for logging, auth, CORS, and custom processing.\n---\n\n`createMiddleware` creates a configuration object for SolidStart that specifies when middleware functions are executed during the request lifecycle.\n\nThere are two lifecycle events available: `onRequest` and `onBeforeResponse`.\n\n- The `onRequest` event is triggered at the beginning of the request lifecycle, before the request is handled by the route handler.\n- The `onBeforeResponse` event is triggered after a request has been processed by the route handler but before the response is sent to the client.\n\n:::note\nSolidStart will only execute the middleware functions if the path to the middleware file is configured within `app.config.ts` using the `middleware` option.\nThis file must export the configuration using `export default`.\n:::\n\nLearn more about middleware in the [Middleware documentation](/solid-start/advanced/middleware).\n\n## Parameters\n\n`createMiddleware` takes an object with the following keys:\n\n- `onRequest` (optional): A middleware function or an array of functions to execute at the `onRequest` event.\n  If an array is provided, the middleware functions will be executed one by one, in the order they appear in the array.\n- `onBeforeResponse` (optional): A middleware function or an array of functions to execute at the `onBeforeResponse` event.\n  If an array is provided, the middleware functions will be executed one by one, in the order they appear in the array.\n\n## Example\n\n```ts title=\"src/middleware/index.ts\"\nimport { createMiddleware } from \"@solidjs/start/middleware\";\n\nexport default createMiddleware({\n\tonRequest: (event) =\u003e {\n\t\tconsole.log(\"Request received:\", event.request.url);\n\t},\n\tonBeforeResponse: (event) =\u003e {\n\t\tconsole.log(\"Sending response:\", event.response.status);\n\t},\n});\n```\n\n```ts title=\"app.config.ts\"\nimport { defineConfig } from \"@solidjs/start/config\";\n\nexport default defineConfig({\n\tmiddleware: \"src/middleware/index.ts\",\n});\n```",
  "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-start/reference/server/create-middleware.mdx",
  "metadata": {
    "path": "src/routes/solid-start/reference/server/create-middleware.mdx",
    "repo": "solidjs/solid-docs",
    "repo_url": "https://github.com/solidjs/solid-docs.git",
    "size": 2293,
    "source_type": "github"
  },
  "hash": "88d65231f6d4125c482c0f00867b90c8c7124aeffb5af57ff8ae97ec624e1df6",
  "timestamp": "2026-02-23T11:43:00.195152216+01:00"
}