@stewie-js/adapter-node
v0.8.0
Published
Node.js HTTP adapter for the Stewie framework
Maintainers
Readme
@stewie-js/adapter-node
❗ Work in progress.
Stewie is under active development and not yet stable. APIs may change between releases. Not recommended for production use yet.
Node.js HTTP adapter for Stewie. Bridges Node's http.IncomingMessage / ServerResponse to the standard Web Request / Response API that @stewie-js/server uses, so the same app handler works across Node, Bun, and other WinterCG runtimes.
Requires Node 18+.
Part of the Stewie framework.
Install
pnpm add @stewie-js/adapter-nodeThe example below also uses @stewie-js/server and @stewie-js/core, which are a common pairing but not required by the adapter itself — it bridges any handler that accepts a Request and returns a Response.
Usage
import { createNodeHandler } from '@stewie-js/adapter-node'
import { renderToString } from '@stewie-js/server'
import { createServer } from 'node:http'
import { readFileSync } from 'node:fs'
import { jsx } from '@stewie-js/core'
import App from './App.js'
const template = readFileSync('dist/client/index.html', 'utf-8')
const handler = createNodeHandler(async (req) => {
const { html, stateScript } = await renderToString(jsx(App, {}))
const page = template
.replace('<!--ssr-outlet-->', html)
.replace('</body>', ` ${stateScript}\n</body>`)
return new Response(page, {
headers: { 'content-type': 'text/html; charset=utf-8' },
})
})
createServer(handler).listen(3000)
console.log('Listening on http://localhost:3000')API
| Export | Description |
|---|---|
| createNodeHandler(app) | Wraps a (Request) => Response \| Promise<Response> function into a http.RequestListener |
| StewieApp | Type: (request: Request) => Response \| Promise<Response> |
Response bodies are streamed through to Node rather than buffered first, so streaming SSR responses from renderToStream work correctly. Unhandled errors thrown by the app handler are caught, logged to console.error, and converted to a 500 response automatically.
