diff options
author | 2023-08-02 06:10:29 +0800 | |
---|---|---|
committer | 2023-08-01 17:10:29 -0500 | |
commit | 4dd6c7900ca40db1b2cebed9bd02a9eb00874d8d (patch) | |
tree | dd011fef12f90190129c3ee5fbfe37f4246786d3 /packages/integrations/node/src/nodeMiddleware.ts | |
parent | e1e958a75860292688569e82b4617fc141056202 (diff) | |
download | astro-4dd6c7900ca40db1b2cebed9bd02a9eb00874d8d.tar.gz astro-4dd6c7900ca40db1b2cebed9bd02a9eb00874d8d.tar.zst astro-4dd6c7900ca40db1b2cebed9bd02a9eb00874d8d.zip |
Fix "res.writeHead is not a function" in Express/node middleware (#7708)
* fix: res.writeHead is not a function
* fix: handler params type added
* fix: handler function params error
* Update packages/integrations/node/src/nodeMiddleware.ts
---------
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/node/src/nodeMiddleware.ts')
-rw-r--r-- | packages/integrations/node/src/nodeMiddleware.ts | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/packages/integrations/node/src/nodeMiddleware.ts b/packages/integrations/node/src/nodeMiddleware.ts index 8d31b6806..fa0cdfda8 100644 --- a/packages/integrations/node/src/nodeMiddleware.ts +++ b/packages/integrations/node/src/nodeMiddleware.ts @@ -1,19 +1,29 @@ import type { NodeApp } from 'astro/app/node'; -import type { IncomingMessage, ServerResponse } from 'node:http'; +import type { ServerResponse } from 'node:http'; import type { Readable } from 'stream'; import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders'; import { responseIterator } from './response-iterator'; -import type { Options } from './types'; +import type { ErrorHandlerParams, Options, RequestHandlerParams } from './types'; // Disable no-unused-vars to avoid breaking signature change // eslint-disable-next-line @typescript-eslint/no-unused-vars export default function (app: NodeApp, _mode: Options['mode']) { - return async function ( - req: IncomingMessage, - res: ServerResponse, - next?: (err?: unknown) => void, - locals?: object - ) { + return async function (...args: RequestHandlerParams | ErrorHandlerParams) { + let error = null; + let [req, res, next, locals] = args as RequestHandlerParams; + + if (args[0] instanceof Error) { + [error, req, res, next, locals] = args as ErrorHandlerParams; + + if (error) { + if (next) { + return next(error); + } else { + throw error; + } + } + } + try { const route = app.match(req); if (route) { |