Skip to content

Repository files navigation

@askrjs/node

CI npm version

Run an @askrjs/server application on Node.js. The adapter translates Node HTTP messages at the boundary while the application continues to use Web Request and Response objects.

Install

npm install @askrjs/server @askrjs/node

Create a Node handler

import { createServer } from "node:http";
import { createServerApp, json } from "@askrjs/server";
import { createNodeHandler } from "@askrjs/node";

const app = createServerApp({
  routes: [{ path: "/health", handler: () => json({ status: "ok" }) }],
});

createServer(createNodeHandler(app, { baseUrl: "http://localhost:3000" })).listen(3000);

createNodeHandler also works as Connect middleware because it accepts an optional next callback. It preserves streaming bodies, repeated Set-Cookie headers, aborts, backpressure, status text, and HEAD responses.

Listen directly

import { listen } from "@askrjs/node";

const server = await listen(app, {
  port: 3000,
  requestTimeout: 120_000,
  headersTimeout: 60_000,
  keepAliveTimeout: 5_000,
});
server.close();

Pass an AbortSignal to integrate shutdown with your process lifecycle.

Enable the built-in ws transport with websocket: true. It defaults to a 1 MiB maximum message payload with compression disabled; pass websocket: { maxPayload, perMessageDeflate } to override those settings.

router.ws("/echo", (socket) => {
  socket.onMessage((message) => socket.send(message));
});

const server = await listen(createServerApp({ router }), { websocket: true });

Route matching, authentication, and middleware complete before the handshake. Rejected upgrades preserve the application response, and shutdown closes active sockets.

Serve a production application

import { serve } from "@askrjs/node";

const running = await serve(app, {
  port: 3000,
  assets: { root: "./dist/client" },
});

await running.close();

serve handles static assets and closes both the HTTP server and the application during shutdown.

MCP over stdio

import { connectMcpStdio } from "@askrjs/node/mcp";

const connection = connectMcpStdio(mcp, { dependencies });
await connection.closed;

Protocol messages use stdin/stdout; diagnostics remain isolated on stderr. Authentication may be provided directly or resolved from the process environment for each message.

About

Node.js HTTP adapter for the transport-neutral Askr server.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages