According TypeScript types definitions, the "IncomingMessage.method" could be undefined:
class IncomingMessage extends stream.Readable {
// ...
method?: string | undefined;
}
What the undefined value tells to us and how me should handle this value?
Currently I do it as below:
import HTTP from "http";
import {
HTTP_StatusCodes,
Logger,
isUndefined,
UnsupportedScenarioError
} from "@yamato-daiwa/es-extensions";
class Server {
private async requestMasterListener(
request: HTTP.IncomingMessage,
rawResponse: HTTP.ServerResponse,
loggingData: {
port: number;
protocol: string;
}
): Promise<void> {
// ...
if (isUndefined(request.method)) {
Logger.logError({
errorType: UnsupportedScenarioError.NAME,
title: UnsupportedScenarioError.DEFAULT_TITLE,
description: "The HTTP method is not defined.",
occurrenceLocation: "Server.requestMasterListener(request, rawResponse)"
});
rawResponse.
writeHead(HTTP_StatusCodes.badRequest, "HTTP method is not specified").
end();
return;
}
// ...
}
}