From 9c15a33b36a45d7eb179394b417f921ad8f060bc Mon Sep 17 00:00:00 2001 From: Mwelwa Nkuta Date: Fri, 19 Jan 2024 13:13:49 +0200 Subject: [PATCH 1/2] docs(learn): Migrate 'Anatomy of an http transaction' guide to learn section (#6226) --- i18n/locales/en.json | 3 ++- navigation.json | 4 ++++ pages/en/guides/index.md | 1 - .../modules}/anatomy-of-an-http-transaction.md | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) rename pages/en/{guides => learn/modules}/anatomy-of-an-http-transaction.md (99%) 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 99% 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..3ce8d59e13908 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 From 26702d206d080753783fe2e1d36d3ad62b73a08b Mon Sep 17 00:00:00 2001 From: Mwelwa Nkuta Date: Sat, 20 Jan 2024 11:19:49 +0200 Subject: [PATCH 2/2] docs(modules): indicating internal node module imports & code blocks definition change --- .../modules/anatomy-of-an-http-transaction.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/pages/en/learn/modules/anatomy-of-an-http-transaction.md b/pages/en/learn/modules/anatomy-of-an-http-transaction.md index 3ce8d59e13908..cc31bd1be7eef 100644 --- a/pages/en/learn/modules/anatomy-of-an-http-transaction.md +++ b/pages/en/learn/modules/anatomy-of-an-http-transaction.md @@ -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('

Hello, World!

'); @@ -233,7 +233,7 @@ response.end(); The `end` function on streams can also take in some optional data to send as the last bit of data on the stream, so we can simplify the example above as follows. -```javascript +```js response.end('

Hello, World!

'); ``` @@ -254,8 +254,8 @@ Building on the earlier example, we're going to make a server that sends back all of the data that was sent to us by the user. We'll format that data as JSON using `JSON.stringify`. -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => { @@ -301,8 +301,8 @@ sends whatever data is received in the request right back in the response. All we need to do is grab the data from the request stream and write that data to the response stream, similar to what we did previously. -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => { @@ -327,8 +327,8 @@ conditions: In any other case, we want to simply respond with a 404. -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => { @@ -360,8 +360,8 @@ is a [`ReadableStream`][] and the `response` object is a [`WritableStream`][]. That means we can use [`pipe`][] to direct data from one to the other. That's exactly what we want for an echo server! -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => { @@ -388,8 +388,8 @@ and message would be. As usual with errors, you should consult the On the response, we'll just log the error to `stderr`. -```javascript -const http = require('http'); +```js +const http = require('node:http'); http .createServer((request, response) => {