diff --git a/i18n/locales/en.json b/i18n/locales/en.json index 4a98b48a82487..1fb6cab852900 100644 --- a/i18n/locales/en.json +++ b/i18n/locales/en.json @@ -78,7 +78,8 @@ "modules": { "links": { "modules": "Modules", - "publishingNodeApiModules": "How to publish a Node-API package" + "publishingNodeApiModules": "How to publish a Node-API package", + "anatomyOfAnHttpTransaction": "Anatomy of an HTTP Transaction" } } }, diff --git a/navigation.json b/navigation.json index 54017f49e8119..25141b733af68 100644 --- a/navigation.json +++ b/navigation.json @@ -212,6 +212,10 @@ "publishingNodeApiModules": { "link": "/learn/modules/publishing-node-api-modules", "label": "components.navigation.learn.modules.links.publishingNodeApiModules" + }, + "anatomyOfAnHttpTransaction": { + "link": "/learn/modules/anatomy-of-an-http-transaction", + "label": "components.navigation.learn.modules.links.anatomyOfAnHttpTransaction" } } } diff --git a/pages/en/guides/index.md b/pages/en/guides/index.md index 615f34caef7b9..a1e004075ccff 100644 --- a/pages/en/guides/index.md +++ b/pages/en/guides/index.md @@ -22,7 +22,6 @@ layout: docs.hbs ## Module-related guides -- [Anatomy of an HTTP Transaction](/guides/anatomy-of-an-http-transaction/) - [Working with Different Filesystems](/guides/working-with-different-filesystems/) - [Backpressuring in Streams](/guides/backpressuring-in-streams/) - [ABI Stability](/guides/abi-stability/) diff --git a/pages/en/guides/anatomy-of-an-http-transaction.md b/pages/en/learn/modules/anatomy-of-an-http-transaction.md similarity index 97% rename from pages/en/guides/anatomy-of-an-http-transaction.md rename to pages/en/learn/modules/anatomy-of-an-http-transaction.md index a701c6d0537a3..cc31bd1be7eef 100644 --- a/pages/en/guides/anatomy-of-an-http-transaction.md +++ b/pages/en/learn/modules/anatomy-of-an-http-transaction.md @@ -1,6 +1,6 @@ --- title: Anatomy of an HTTP Transaction -layout: docs.hbs +layout: learn.hbs --- # Anatomy of an HTTP Transaction @@ -17,8 +17,8 @@ the API docs for each of those. Any node web server application will at some point have to create a web server object. This is done by using [`createServer`][]. -```javascript -const http = require('http'); +```js +const http = require('node:http'); const server = http.createServer((request, response) => { // magic happens here! @@ -31,7 +31,7 @@ handler. In fact, the [`Server`][] object returned by [`createServer`][] is an [`EventEmitter`][], and what we have here is just shorthand for creating a `server` object and then adding the listener later. -```javascript +```js const server = http.createServer(); server.on('request', (request, response) => { // the same kind of magic happens here! @@ -53,7 +53,7 @@ When handling a request, the first thing you'll probably want to do is look at the method and URL, so that appropriate actions can be taken. Node.js makes this relatively painless by putting handy properties onto the `request` object. -```javascript +```js const { method, url } = request; ``` @@ -66,7 +66,7 @@ everything after and including the third forward slash. Headers are also not far away. They're in their own object on `request` called `headers`. -```javascript +```js const { headers } = request; const userAgent = headers['user-agent']; ``` @@ -92,7 +92,7 @@ The chunk emitted in each `'data'` event is a [`Buffer`][]. If you know it's going to be string data, the best thing to do is collect the data in an array, then at the `'end'`, concatenate and stringify it. -```javascript +```js let body = []; request .on('data', chunk => { @@ -121,7 +121,7 @@ _thrown_, which could crash your Node.js program.** You should therefore add an continue on your way. (Though it's probably best to send some kind of HTTP error response. More on that later.) -```javascript +```js request.on('error', err => { // This prints the error message and stack trace to `stderr`. console.error(err.stack); @@ -138,8 +138,8 @@ At this point, we've covered creating a server, and grabbing the method, URL, headers and body out of requests. When we put that all together, it might look something like this: -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => { @@ -176,7 +176,7 @@ be 200. Of course, not every HTTP response warrants this, and at some point you'll definitely want to send a different status code. To do that, you can set the `statusCode` property. -```javascript +```js response.statusCode = 404; // Tell the client that the resource wasn't found. ``` @@ -186,7 +186,7 @@ There are some other shortcuts to this, as we'll see soon. Headers are set through a convenient method called [`setHeader`][]. -```javascript +```js response.setHeader('Content-Type', 'application/json'); response.setHeader('X-Powered-By', 'bacon'); ``` @@ -206,7 +206,7 @@ If you want, you can _explicitly_ write the headers to the response stream. To do this, there's a method called [`writeHead`][], which writes the status code and the headers to the stream. -```javascript +```js response.writeHead(200, { 'Content-Type': 'application/json', 'X-Powered-By': 'bacon', @@ -221,7 +221,7 @@ start sending response data. Since the `response` object is a [`WritableStream`][], writing a response body out to the client is just a matter of using the usual stream methods. -```javascript +```js response.write(''); response.write('
'); response.write('