Repository: withastro/astro
File: packages/integrations/node/src/serve-app.ts (TypeScript)

Code before:
import { AsyncLocalStorage } from 'node:async_hooks';
import { NodeApp } from 'astro/app/node';
import type { Options, RequestHandler } from './types.js';

/**
 * Creates a Node.js http listener for on-demand rendered pages, compatible with http.createServer and Connect middleware.
 * If the next callback is provided, it will be called if the request does not have a matching route.
 * Intended to be used in both standalone and middleware mode.
 */
export function createAppHandler(app: NodeApp, options: Options): RequestHandler {
	/**
	 * Keep track of the current request path using AsyncLocalStorage.
	 * Used to log unhandled rejections with a helpful message.
	 */
	const als = new AsyncLocalStorage<string>();
	const logger = app.getAdapterLogger();
	process.on('unhandledRejection', (reason) => {
		const requestUrl = als.getStore();
		logger.error(`Unhandled rejection while rendering ${requestUrl}`);
		console.error(reason);
	});

	const prerenderedErrorPageFetch = options.experimentalErrorPageHost
		? (url: string) => {
				const errorPageUrl = new URL(url);
				const originUrl = new URL(options.experimentalErrorPageHost!);
				errorPageUrl.protocol = originUrl.protocol;
				errorPageUrl.host = originUrl.host;
				return fetch(errorPageUrl);
			}
		: undefined;

	return async (req, res, next, locals) => {
		let request: Request;
		try {
			request = NodeApp.createRequest(req);
		} catch (err) {
			logger.error(`Could not render ${req.url}`);
			console.error(err);
			res.statusCode = 500;
			res.end('Internal Server Error');
			return;
		}

		const routeData = app.match(request);
		if (routeData) {
			const response = await als.run(request.url, () =>
				app.render(request, {
					addCookieHeader: true,
					locals,
					routeData,

Review comment (suggestion):
You should use `URL.canParse` beforehand. If the function returns `false`, we could throw an error or default to `undefined`. Probably an error makes sense. Perhaps we should validate outside of this closure. 

Code after:
import { AsyncLocalStorage } from 'node:async_hooks';
import { NodeApp } from 'astro/app/node';
import type { Options, RequestHandler } from './types.js';

/**
 * Creates a Node.js http listener for on-demand rendered pages, compatible with http.createServer and Connect middleware.
 * If the next callback is provided, it will be called if the request does not have a matching route.
 * Intended to be used in both standalone and middleware mode.
 */
export function createAppHandler(app: NodeApp, options: Options): RequestHandler {
	/**
	 * Keep track of the current request path using AsyncLocalStorage.
	 * Used to log unhandled rejections with a helpful message.
	 */
	const als = new AsyncLocalStorage<string>();
	const logger = app.getAdapterLogger();
	process.on('unhandledRejection', (reason) => {
		const requestUrl = als.getStore();
		logger.error(`Unhandled rejection while rendering ${requestUrl}`);
		console.error(reason);
	});

	const originUrl = options.experimentalErrorPageHost
		? new URL(options.experimentalErrorPageHost)
		: undefined;

	const prerenderedErrorPageFetch = originUrl
		? (url: string) => {
				const errorPageUrl = new URL(url);
				errorPageUrl.protocol = originUrl.protocol;
				errorPageUrl.host = originUrl.host;
				return fetch(errorPageUrl);
			}
		: undefined;

	return async (req, res, next, locals) => {
		let request: Request;
		try {
			request = NodeApp.createRequest(req);
		} catch (err) {
			logger.error(`Could not render ${req.url}`);
			console.error(err);
			res.statusCode = 500;
			res.end('Internal Server Error');
			return;
		}

		const routeData = app.match(request);
		if (routeData) {
			const response = await als.run(request.url, () =>
				app.render(request, {