@nwire/express
v0.15.1
Published
Nwire — Express-backed HTTP adopter. expressAdapter() consumes wires with binding.$adapter==='http' and mounts them on an Express server; fromExpressMiddleware() bridges Express middleware into httpKoa's middleware chain.
Readme
@nwire/express
Express-backed HTTP adopter — same wires as
@nwire/koa, different runtime.
pnpm add @nwire/express @nwire/app @nwire/endpoint @nwire/wiresexpress@^4 || ^5 is a peer dep.
Two integration modes
1. Drive Nwire as the HTTP server (Adapter mode)
import { createApp } from "@nwire/app";
import { endpoint } from "@nwire/endpoint";
import { post } from "@nwire/wires/http";
import { expressAdapter } from "@nwire/express";
import { z } from "zod";
const app = createApp({ appName: "api" });
app.wire(post("/hello", { body: z.object({ name: z.string() }) }), async (input) => ({
message: `Hello, ${input.name}!`,
}));
await endpoint("api", { port: 3000 })
.use(expressAdapter({ prefix: "/api" }))
.mount(app)
.run();2. Mount Nwire wires inside an existing Express app
When you already run Express (or NestJS, which uses Express under the
hood), nwireToExpressRouter extracts wired routes as an Express Router
you mount alongside legacy handlers. Migration without rewrite.
import express from "express";
import { createApp } from "@nwire/app";
import { nwireToExpressRouter } from "@nwire/express";
const legacy = express();
legacy.get("/legacy", legacyHandler);
const nwireApp = createApp({ appName: "orders" });
nwireApp.wire(/* ... */);
await nwireApp.start();
legacy.use("/api/nwire", nwireToExpressRouter(nwireApp));
legacy.listen(3000);The router handles its own JSON body-parsing (bodyParser: false opts
out) so it works whether Express has body-parser globally or not. See
examples/nest-interop for the full
Nwire-inside-NestJS pattern.
Adopter config
expressAdapter({
port: 3000, // 0 = ephemeral; .port() returns the bound port
host: "0.0.0.0",
prefix: "/api",
middleware: [helmet(), morgan("combined")],
logger: myLogger,
});Bridging middleware
fromExpressMiddleware(mw) wraps an Express middleware so it can be
passed in httpKoa({ middleware: [...] }) — useful when an Express-only
middleware (e.g., a vendored auth check) needs to run inside a Koa-backed
adopter.
Related
@nwire/koa— Koa-backed sibling adopter@nwire/endpoint— the lifecycle hostexamples/nest-interop— Nwire inside NestJS
