---
url: >-
  https://lit-ui-router.dev/reference/ui-router-server/connect/functions/createConnectMiddleware.md
---
[ui-router-server](../../index.md) / [connect](../index.md) / createConnectMiddleware

# Function: createConnectMiddleware()

```ts
function createConnectMiddleware(router, options?): ConnectMiddleware;
```

Defined in: [connect.ts:232](https://github.com/simshanith/lit-ui-router/blob/740888c2b2ba358c4dfa52a122e927df6a604b12/packages/ui-router-server/src/connect.ts#L232)

Wraps a [ServerRouter](../../index/interfaces/ServerRouter.md) as Connect middleware. Mount it BEFORE the
static layer; the default host serves shells by rewriting `req.url` and
`next()`ing into it. `resolve()` is always async (the simulate tier's
lazy boundary), which Connect absorbs naturally — with matcher-tier
mounts nothing async ever actually runs.

Under a [base](../interfaces/ConnectAdapterOptions.md#base), the mounts see each
url with the base taken off, and every url the middleware writes gets it
back.

* `redirect` → `writeHead(302, { Location })`, the request's search
  MERGED into the verdict's location via [mergeSearch](../../index/functions/mergeSearch.md) (targets that
  declare their own query keep it — never `?a=1?b=2`).
* `shell` → `Link: <mount>; rel="canonical"` (the shell is the same
  representation at every route path), then [serveShell](../interfaces/ConnectAdapterOptions.md#serveshell).
* `shell` with status (the otherwise projection) → validators stripped,
  downstream status relabeled, no canonical Link (a 404 is not an
  alternate representation of the mount root), then [serveShell](../interfaces/ConnectAdapterOptions.md#serveshell).
* `notFound` with a mount → [serveNotFound](../interfaces/ConnectAdapterOptions.md#servenotfound) (honest 404).
* `notFound` without a mount, or a non-navigation request → `next()`.
* `delegate` → `next()` with [\`delegates: 'next'\`](../interfaces/ConnectAdapterOptions.md#delegates); mount the
  handlers for delegating states downstream.
* `response` → `next()` with [\`responses: 'next'\`](../interfaces/ConnectAdapterOptions.md#responses); answer
  response states downstream.

## Parameters

| Parameter | Type |
| ------ | ------ |
| `router` | [`ServerRouter`](../../index/interfaces/ServerRouter.md) |
| `options?` | [`ConnectAdapterOptions`](../interfaces/ConnectAdapterOptions.md) |

## Returns

[`ConnectMiddleware`](../type-aliases/ConnectMiddleware.md)

## Throws

at construction when the router names delegate keys and
[delegates](../interfaces/ConnectAdapterOptions.md#delegates) is not `'next'`, or
declares response states and [responses](../interfaces/ConnectAdapterOptions.md#responses) is not `'next'`
