> ## Content Index
> Fetch the complete content index at: https://logbuch-dev-blog.nichedthemes.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Reading the request
- URL: https://logbuch-dev-blog.nichedthemes.com/reading-the-request/
- Published: 2026-09-21T07:30:00.000Z
- Updated: 2026-09-21T07:30:00.000Z
- Description: Method, path, headers and body, turned into a standard Request. The server still answers everything with the same text.
- Author: Mira Okafor
- Tags: Backend, TypeScript, A tiny HTTP server in TypeScript, #Import 2026-11-19 07:37

The server from chapter 1 ignores what it is asked. This chapter reads method, path, headers and body, and turns them into a `Request` object, the same one `fetch` uses. Every chapter after this works with `Request` and `Response` only.

## From IncomingMessage to Request

```ts
import type { IncomingMessage } from 'node:http';
import { Readable } from 'node:stream';

export function toRequest(msg: IncomingMessage): Request {
  const url = `http://${msg.headers.host}${msg.url}`;
  const body = ['GET', 'HEAD'].includes(msg.method!) ? null : (Readable.toWeb(msg) as ReadableStream);
  return new Request(url, { method: msg.method, headers: msg.headers as HeadersInit, body, duplex: 'half' });
}
```

`duplex: 'half'` is required by Node when a request has a streamed body. Leave it out and it throws, with a message that names the option.

## Still the same answer

```console
$ curl -s localhost:8080/anything
Hello
$ curl -s -X POST localhost:8080/else -d x=1
Hello
```

Same text for every path. Chapter 3 changes that.