Executing custom code
All JavaScript code is executed by the main thread, which means that any operation that doesn’t use the non-blocking API provided by Node.js will block the thread. For the sake of consistency, add the statement shown in Listing 4.18 to the promises.ts
file to wrap the write
method defined by the ServerResponse
class in a promise.
Listing 4.18: Adding a Function in the promises.ts File in the src Folder
import { ServerResponse } from "http";
import { promisify } from "util";
export const endPromise = promisify(ServerResponse.prototype.end) as
(data: any) => Promise<void>;
export const writePromise = promisify(ServerResponse.prototype.write) as
(data: any) => Promise<void>;
Listing 4.19 filters out the requests for the favicon.ico
file, which was fine in earlier examples, but will add unwanted requests in this section.
Listing 4.19: Filtering Requests in the server.ts File in the src Folder
...