Node.js adapter for Astro framework enabling server-side rendering and standalone server deployments
npx @tessl/cli install tessl/npm-astrojs--node@9.4.0@astrojs/node is a Node.js adapter for the Astro framework that enables server-side rendering (SSR) deployments to Node.js environments. It provides functionality to deploy Astro sites as standalone Node.js applications or middleware, supporting both hybrid and server-side rendering modes with session management, static asset serving, and comprehensive server configuration options.
npm install @astrojs/nodeimport node from "@astrojs/node";For server runtime exports:
import { createExports, start } from "@astrojs/node/server.js";For preview server:
import createPreviewServer from "@astrojs/node/preview.js";For direct access to named exports:
import { createIntegration, getAdapter } from "@astrojs/node";import { defineConfig } from "astro/config";
import node from "@astrojs/node";
export default defineConfig({
output: "server",
adapter: node({
mode: "standalone"
})
});import { defineConfig } from "astro/config";
import node from "@astrojs/node";
export default defineConfig({
output: "server",
adapter: node({
mode: "middleware"
})
});import express from "express";
import { createExports } from "./dist/server.js";
const app = express();
const { handler } = createExports(manifest, options);
// Use Astro as middleware
app.use(handler);
app.listen(3000);@astrojs/node is built around several key components:
Main integration factory function for configuring the @astrojs/node adapter with mode selection, experimental features, and build hooks.
/** Default export - primary way to use the adapter */
export default function createIntegration(userOptions: UserOptions): AstroIntegration;
/** Named export - alternative import method */
export function createIntegration(userOptions: UserOptions): AstroIntegration;
interface UserOptions {
/** Deployment mode: middleware or standalone server */
mode: 'middleware' | 'standalone';
/** Disable HTML streaming for hosting constraints */
experimentalDisableStreaming?: boolean;
/** Enable static headers in framework API file */
experimentalStaticHeaders?: boolean;
/** Host for fetching prerendered error pages */
experimentalErrorPageHost?: string | URL;
}
interface AstroIntegration {
name: string;
hooks: {
'astro:config:setup': (params: ConfigSetupParams) => Promise<void>;
'astro:build:generated': (params: BuildGeneratedParams) => void;
'astro:config:done': (params: ConfigDoneParams) => void;
'astro:build:done': () => Promise<void>;
};
}Server runtime functions for creating request handlers, starting servers, and managing the application lifecycle in production.
function createExports(
manifest: SSRManifest,
options: Options
): {
options: Options;
handler: RequestHandler;
startServer: () => ServerResult;
};
function start(manifest: SSRManifest, options: Options): void;
interface Options extends UserOptions {
host: string | boolean;
port: number;
server: string;
client: string;
assets: string;
trailingSlash?: SSRManifest['trailingSlash'];
experimentalStaticHeaders: boolean;
}Handler creation functions for different deployment modes including middleware, standalone, and static file serving.
function createMiddleware(app: NodeApp, options: Options): RequestHandler;
function createStandaloneHandler(
app: NodeApp,
options: Options
): (req: http.IncomingMessage, res: http.ServerResponse) => void;
function createAppHandler(app: NodeApp, options: Options): RequestHandler;
function createStaticHandler(
app: NodeApp,
options: Options
): (req: IncomingMessage, res: ServerResponse, ssr: () => unknown) => void;
type RequestHandler = (
req: IncomingMessage,
res: ServerResponse,
next?: (err?: unknown) => void,
locals?: object
) => void | Promise<void>;Development preview server functionality for testing built applications locally with configurable host and port options.
function createPreviewServer(preview: PreviewParams): Promise<PreviewServer>;
interface PreviewParams {
serverEntrypoint: URL;
host?: string;
port?: number;
headers?: Record<string, string>;
logger: AstroIntegrationLogger;
}
interface PreviewServer {
host: string;
port: number;
closed(): Promise<void>;
stop(): Promise<void>;
}Server creation and management utilities for HTTP/HTTPS servers with graceful shutdown and networking features.
function createServer(
listener: http.RequestListener,
host: string,
port: number
): PreviewServer & { server: http.Server | https.Server };
function standalone(
app: NodeApp,
options: Options
): {
server: PreviewServer & { server: http.Server | https.Server };
done: Promise<void>;
};
function hostOptions(host: Options['host']): string;
function logListeningOn(
logger: AstroIntegrationLogger,
server: http.Server | https.Server,
configuredHost: string | boolean | undefined
): Promise<void>;Node.js runtime polyfills and shared constants used across the adapter implementation.
/** Applies Node.js polyfills for Astro runtime compatibility */
function applyPolyfills(): void;
/** Static headers JSON file name constant */
const STATIC_HEADERS_FILE: '_experimentalHeaders.json';The adapter automatically applies polyfills during server initialization to ensure compatibility between Astro's runtime expectations and Node.js environment capabilities.
@astrojs/node respects several environment variables for runtime configuration:
interface SSRManifest {
trailingSlash?: 'always' | 'never' | 'ignore';
outDir: string | URL;
// ... other Astro SSR manifest properties
}
interface NodeApp {
getAdapterLogger(): AstroIntegrationLogger;
match(request: Request, prerender?: boolean): RouteData | undefined;
render(request: Request | IncomingMessage, options?: RenderOptions): Promise<Response>;
removeBase(pathname: string): string;
headersMap?: NodeAppHeadersJson;
setHeadersMap(headers: NodeAppHeadersJson): void;
}
type NodeAppHeadersJson = Array<{
pathname: string;
headers: Array<{ key: string; value: string }>;
}>;
interface AstroAdapter {
name: string;
serverEntrypoint: string;
previewEntrypoint: string;
exports: string[];
args: Options;
adapterFeatures: AdapterFeatures;
supportedAstroFeatures: SupportedFeatures;
}
interface AdapterFeatures {
buildOutput: 'server';
edgeMiddleware: false;
experimentalStaticHeaders: boolean;
}
interface SupportedFeatures {
hybridOutput: 'stable';
staticOutput: 'stable';
serverOutput: 'stable';
sharpImageService: 'stable';
i18nDomains: 'experimental';
envGetSecret: 'stable';
}
interface RenderOptions {
addCookieHeader?: boolean;
locals?: object;
routeData?: RouteData;
prerenderedErrorPageFetch?: (url: string) => Promise<Response>;
}
interface RouteData {
route: string;
prerender?: boolean;
pathname?: string;
// ... other Astro route data properties
}