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>
This commit is contained in:
parent
e1e958a758
commit
4dd6c7900c
3 changed files with 34 additions and 8 deletions
5
.changeset/olive-knives-rush.md
Normal file
5
.changeset/olive-knives-rush.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/node': patch
|
||||
---
|
||||
|
||||
fix issuse #7590 "res.writeHead is not a function" in Express/Node middleware
|
|
@ -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) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { IncomingMessage, ServerResponse } from 'node:http';
|
||||
|
||||
export interface UserOptions {
|
||||
/**
|
||||
* Specifies the mode that the adapter builds to.
|
||||
|
@ -14,3 +16,12 @@ export interface Options extends UserOptions {
|
|||
server: string;
|
||||
client: string;
|
||||
}
|
||||
|
||||
export type RequestHandlerParams = [
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
next?: (err?: unknown) => void,
|
||||
locals?: object
|
||||
];
|
||||
|
||||
export type ErrorHandlerParams = [unknown, ...RequestHandlerParams];
|
||||
|
|
Loading…
Reference in a new issue