<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by KAN Uiiu on Medium]]></title>
        <description><![CDATA[Stories by KAN Uiiu on Medium]]></description>
        <link>https://medium.com/@jianweiyouu?source=rss-b3d010adcff9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Tfe_Nx0gckhLYX9B</url>
            <title>Stories by KAN Uiiu on Medium</title>
            <link>https://medium.com/@jianweiyouu?source=rss-b3d010adcff9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:47:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jianweiyouu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[HTTP Request from Frontend: Understanding Fetch API]]></title>
            <link>https://medium.com/@jianweiyouu/http-request-from-frontend-understanding-fetch-api-6edc107b77f2?source=rss-b3d010adcff9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6edc107b77f2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[https]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[KAN Uiiu]]></dc:creator>
            <pubDate>Tue, 22 Sep 2026 05:29:51 GMT</pubDate>
            <atom:updated>2026-09-22T05:29:51.078Z</atom:updated>
            <content:encoded><![CDATA[<p>Thanks to the HTTP protocol, we can send a request from the client side (frontend) and receive a response from the server (backend). Modern browsers provide the <strong>Fetch API</strong>, which allows us to send requests to a specific URL on the server. An HTTP request consists of three main parts: the <strong>Request Line</strong>, <strong>Headers</strong>, and <strong>Body</strong>.</p><h3>Request Line</h3><p>The Request Line includes a pair of URL and HTTP method.</p><h3>URL</h3><p>The URL determines which server we are sending the request to. A typical URL consists of a protocol, domain name, path (routes), and query parameters.</p><h3>Method</h3><p>The HTTP method specifies the action or operation we want to perform. Common methods include GET, POST, PUT, PATCH, and DELETE.</p><p>In JavaScript, the <strong>URL</strong> is passed as the first argument to fetch(), while the <strong>Method</strong> is defined inside the configuration object (options) as the second argument:</p><pre>// A simple GET request (GET is the default method if omitted)<br>fetch(&quot;https://api.example.com/v1/users?role=admin&quot;, {<br>  method: &quot;GET&quot;,<br>});</pre><h3>Headers</h3><p>HTTP Headers are key-value pairs that contain metadata about the request. They provide context so the server knows how to process the request correctly.</p><p>In fetch(), headers are defined as a key-value object inside the headers property of the configuration object.</p><h3>Common Headers in fetch()</h3><ul><li><strong>Content-Type</strong>: Informs the server about the format of the request body (e.g., &#39;application/json&#39;).</li><li><strong>Authorization</strong>: Passes credentials, such as a Bearer Token (&#39;Bearer &lt;your_token&gt;&#39;), to prove identity for protected routes.</li></ul><pre>fetch(&quot;https://api.example.com/v1/users&quot;, {<br>  method: &quot;POST&quot;,<br>  headers: {<br>    &quot;Content-Type&quot;: &quot;application/json&quot;,<br>    Authorization: &quot;Bearer token_xyz123&quot;,<br>  },<br>});</pre><h3>Body</h3><p>The Request Body contains the actual payload (data) sent to the server. It is optional and depends heavily on the HTTP method being used.</p><p>In fetch(), the body must be passed under the body property. Since the HTTP protocol transmits data as strings over the network, JavaScript objects must be serialized into a JSON string using JSON.stringify().</p><h3>Usage in fetch()</h3><ul><li><strong>POST / </strong><strong>PUT / </strong><strong>PATCH</strong>: Require a body property containing serialized data.</li><li><strong>GET / </strong><strong>DELETE</strong>: Should <strong>not</strong> include a body property. Parameters should be appended directly to the URL query string instead.</li></ul><pre>// Complete POST request with Body<br>fetch(&quot;https://api.example.com/v1/users&quot;, {<br>  method: &quot;POST&quot;,<br>  headers: {<br>    &quot;Content-Type&quot;: &quot;application/json&quot;,<br>  },<br>  body: JSON.stringify({<br>    name: &quot;Alex&quot;,<br>    email: &quot;alex@example.com&quot;,<br>  }),<br>});</pre><h3>Putting It All Together</h3><p>Now we know a HTTP request should include url, method, headers and sometimes with body. So let’s put these HTTP information into fetch() syntax:</p><pre>fetch(URL, {<br>  method: METHOD, // e.g., &#39;GET&#39;, &#39;POST&#39;, &#39;PUT&#39;, &#39;DELETE&#39;<br>  headers: {<br>    // HEADERS (Metadata)<br>    &quot;Content-Type&quot;: &quot;application/json&quot;,<br>    Authorization: &quot;Bearer &lt;token&gt;&quot;,<br>  },<br>  body: JSON.stringify(DATA), // BODY (Payload - for POST/PUT/PATCH)<br>});</pre><h3>Completing the Data Cycle: Receiving the Response</h3><p>Sending the payload to the server is only half the battle. Once the server processes our request, it returns a response — typically containing the newly created resource, a success status, or generated IDs (e.g., { id: 101, name: &quot;Alex&quot;, email: &quot;alex@example.com&quot; }).</p><p>To complete the data cycle and render the changes on the screen, we must handle the response and update our frontend state accordingly:</p><pre>let userState = null;<br>// 1. Send the request (Outbound)<br>fetch(&quot;https://api.example.com/v1/users&quot;, {<br>  method: &quot;POST&quot;,<br>  headers: {<br>    &quot;Content-Type&quot;: &quot;application/json&quot;,<br>  },<br>  body: JSON.stringify({<br>    name: &quot;Alex&quot;,<br>    email: &quot;alex@example.com&quot;,<br>  }),<br>})<br>  // 2. Parse the server response stream into JSON (Inbound)<br>  .then((res) =&gt; {<br>    if (!res.ok) {<br>      throw new Error(`Failed to create user: ${res.status}`);<br>    }<br>    return res.json();<br>  })<br>  // 3. Connect the returned data to the Frontend State &amp; UI<br>  .then((newUser) =&gt; {<br>    userState = newUser;<br>    <br>    const nameEl = document.querySelector(&quot;#user-name&quot;);<br>    <br>    if(nameEl){<br>        nameEl.textContent = userState.name;<br>    }<br>  })<br>  .catch((err) =&gt; {<br>    console.error(&quot;POST Request Error:&quot;, err);<br>  });</pre><h3>Conclusion</h3><p>Mastering how HTTP elements map to the fetch() API allows frontend developers to easily configure API requests, inspect responses, and manage UI states based on backend data structures.</p><h3>Before You Leave</h3><p>If you find this article helpful or maybe inspiring, please leave a comment before you leave. Or at least give this a like. Also, feel free to subscribe my <a href="https://hackmd.io/@HH7gX6QXRwuPW5_lHiQ7ZA">HackMD</a> and <a href="https://medium.com/@jianweiyouu">Medium</a> for more aritcles later.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6edc107b77f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node.js: How HTTP Server Inherits Properties from NET Server?]]></title>
            <link>https://medium.com/@jianweiyouu/node-js-how-http-server-inherits-properties-from-net-server-0016bb842763?source=rss-b3d010adcff9------2</link>
            <guid isPermaLink="false">https://medium.com/p/0016bb842763</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[http-server]]></category>
            <dc:creator><![CDATA[KAN Uiiu]]></dc:creator>
            <pubDate>Tue, 18 Aug 2026 03:44:42 GMT</pubDate>
            <atom:updated>2026-08-18T03:44:42.058Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p><a href="https://medium.com/@jianweiyouu/not-christopher-nolans-epic-but-my-own-odyssey-of-node-js-88a88cf8638e">Last time</a>, we traced the createServer function all the way from http.js to net.js. Through our exploration, we identified two core Server functions:</p><ul><li>One in _http_server.js (which we call the <strong>HTTP Server</strong> function)</li><li>The other in net.js (which we call the <strong>NET Server</strong> function)</li></ul><p>Today, let me walk you through how the HTTP Server inherits instance properties directly from the NET Server.</p><h3>HTTP Server Function</h3><p>The HTTP Server function is defined in <a href="https://github.com/nodejs/node/blob/main/lib/_http_server.js">node/lib/_http_server.js</a>:</p><pre>function Server(options, requestListener) {<br>  /* ... Omitted logic ... */  <br><br>  net.Server.call(<br>    this,<br>    { <br>      allowHalfOpen: true, <br>      noDelay: options.noDelay ?? true,<br>      keepAlive: options.keepAlive,<br>      keepAliveInitialDelay: options.keepAliveInitialDelay,<br>      highWaterMark: options.highWaterMark <br>    }<br>  );<br>  /* ... Omitted logic ... */<br>}</pre><p>As shown above, we access the NET Server constructor function from the net module directly inside the HTTP Server constructor.</p><p>We are able to use the net module here because it was imported at the top of the file:</p><pre>const net = require(&#39;net&#39;);</pre><h3>NET Server Function</h3><p>The NET Server function is defined in <a href="https://github.com/nodejs/node/blob/main/lib/net.js">node/lib/net.js</a>:</p><pre>function Server(options, connectionListener) {<br>  /* ... Omitted logic ... */<br>  this.allowHalfOpen = options.allowHalfOpen || false;<br>  this.pauseOnConnect = !!options.pauseOnConnect;<br>  this.noDelay = Boolean(options.noDelay);<br>  this.keepAlive = Boolean(options.keepAlive);<br>  this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);<br>  this.highWaterMark = options.highWaterMark ?? getDefaultHighWaterMark();<br>  <br>  if (options.blockList) {<br>    if (!module.exports.BlockList.isBlockList(options.blockList)) {<br>      throw new ERR_INVALID_ARG_TYPE(&#39;options.blockList&#39;, &#39;net.BlockList&#39;, options.blockList);<br>    }<br>    this.blockList = options.blockList;<br>  }<br>}</pre><h3>How net.Server.call(this, ...) Works</h3><p>Now that we can see both functions, how do they actually connect under the hood? The magic happens right at net.Server.call(...).</p><p>This single line achieves two vital goals simultaneously:</p><h3>1. Passing Arguments Down to NET Server</h3><p>Instead of re-implementing TCP configurations in _http_server.js, the HTTP Server prepares a tailored options object (e.g., { allowHalfOpen: true, noDelay: ... }) and passes it down as arguments to the NET Server constructor.</p><h3>2. Borrowing Constructor for Property Inheritance (Constructor Stealing)</h3><p>In JavaScript, functions are objects with built-in utility methods inherited from Function.prototype. The .call() method allows us to invoke a function while explicitly specifying its this context.</p><p>When net.Server.call(this, options) is executed:</p><ul><li><strong>The </strong>this<strong> Swap</strong>: The this inside net.Server is forced to point to the <strong>newly created HTTP Server instance</strong>.</li><li><strong>Property Injection</strong>: As net.Server executes line by line (this.allowHalfOpen = ..., this.noDelay = ...), it directly attaches all these TCP-related properties onto the <strong>HTTP Server instance</strong>.</li></ul><pre>[ HTTP Server Instance (this) ]<br>            │<br>            ▼  &lt;-- Passed into net.Server.call(this, options)<br>┌─────────────────────────────────────────┐<br>│ net.Server executes:                    │<br>│   this.allowHalfOpen = options.allow... │<br>│   this.noDelay = options.noDelay        │<br>└─────────────────────────────────────────┘<br>            │<br>            ▼  <br>[ HTTP Server Instance now holds all NET Server Properties! ]</pre><blockquote><strong><em>Key Takeaway</em></strong></blockquote><blockquote><em>Through </em><em>.call(this, options), the HTTP Server </em><strong><em>passes configurations down</em></strong><em> to the NET Server while </em><strong><em>borrowing its constructor</em></strong><em> to initialize and populate TCP properties on its own instance.</em></blockquote><blockquote>I am going to share those detailed options in the NET Server soon. Stay tuned!</blockquote><h3>Before You Leave</h3><p>If you found this article helpful or inspiring, please leave a comment or give it a few claps 👏. Also, feel free to follow my <a href="https://hackmd.io/@HH7gX6QXRwuPW5_lHiQ7ZA"><strong>HackMD</strong></a> and <a href="https://medium.com/@jianweiyouu/about"><strong>Medium</strong></a> for more deep dives into Node.js internals later!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0016bb842763" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Not Christopher Nolan’s Epic, But My Own Odyssey of Node.js]]></title>
            <link>https://medium.com/@jianweiyouu/not-christopher-nolans-epic-but-my-own-odyssey-of-node-js-88a88cf8638e?source=rss-b3d010adcff9------2</link>
            <guid isPermaLink="false">https://medium.com/p/88a88cf8638e</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[KAN Uiiu]]></dc:creator>
            <pubDate>Thu, 13 Aug 2026 03:53:25 GMT</pubDate>
            <atom:updated>2026-08-13T03:53:25.167Z</atom:updated>
            <content:encoded><![CDATA[<p>As a front-end developer, I was always curious about how to actually create a server. In my daily work, I often interact with APIs, but I had never really built one myself.</p><p>So, I followed some tutorials, and here is how I began:</p><pre>const http = require(&#39;node:http&#39;);<br><br>const server = http.createServer((req, res) =&gt; {<br>  if (req.url === &quot;/&quot;) {<br>    res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/plain&#39; });<br>    res.end(&#39;Welcome to Home Page&#39;);<br>  } else if (req.url === &quot;/about&quot;) {<br>    res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/plain&#39; });<br>    res.end(&#39;About Us Page&#39;);<br>  } else {<br>    res.writeHead(404, { &#39;Content-Type&#39;: &#39;text/plain&#39; });<br>    res.end(&#39;Page Not Found&#39;);<br>  }<br>});<br><br>server.listen(3000, () =&gt; {<br>  console.log(&quot;Server running at http://localhost:3000/&quot;);<br>});</pre><p>Okay, now it seems to work perfectly. We require (or import) http and use its methods, and everything works fine. However, before moving on, I wanted to understand more about what is actually inside the http module and how it is designed. How is the createServer method actually defined?</p><h3>node/lib/http.js</h3><p>I then went to check where the source code of this http module lives in Node.js. (You can take a look at it <a href="https://github.com/nodejs/node/blob/main/lib/http.js">here</a>).</p><p>Just like how we export functions or components in front-end development, in http.js, they use module.exports to expose various methods and classes—including createServer—so that external files can consume them:</p><pre>module.exports = {<br>  _connectionListener,<br>  METHODS: methods.toSorted(),<br>  STATUS_CODES,<br>  Agent: httpAgent.Agent,<br>  ClientRequest,<br>  IncomingMessage,<br>  OutgoingMessage,<br>  Server,<br>  ServerResponse,<br>  createServer,<br>  validateHeaderName,<br>  validateHeaderValue,<br>  get,<br>  request,<br>  setMaxIdleHTTPParsers(max) {<br>    validateInteger(max, &#39;max&#39;, 1);<br>    parsers.max = max;<br>  },<br>  setGlobalProxyFromEnv,<br>};</pre><p>When we look into how createServer is defined, we find that it simply instantiates the Server class:</p><pre>function createServer(opts, requestListener) {<br>  return new Server(opts, requestListener);<br>}</pre><p>So, where does this Server class come from? Scrolling back to the top of http.js, we see it&#39;s imported from another file named _http_server.js:</p><pre>const {<br>  _connectionListener,<br>  STATUS_CODES,<br>  Server,<br>  ServerResponse,<br>} = require(&#39;_http_server&#39;);</pre><h3>node/lib/_http_server.js</h3><p>Let’s head over to <a href="https://github.com/nodejs/node/blob/main/lib/_http_server.js">node/lib/_http_server.js</a> to check how the HTTP Server constructor is designed:</p><pre>function Server(options, requestListener) {<br>  if (!(this instanceof Server)) return new Server(options, requestListener);<br>  <br>  if (typeof options === &#39;function&#39;) {<br>    requestListener = options;<br>    options = kEmptyObject;<br>  } else if (options == null) {<br>    options = kEmptyObject;<br>  } else {<br>    validateObject(options, &#39;options&#39;);<br>  }<br>  <br>  storeHTTPOptions.call(this, options);<br><br>  net.Server.call(<br>    this,<br>    { allowHalfOpen: true, noDelay: options.noDelay ?? true,<br>      keepAlive: options.keepAlive,<br>      keepAliveInitialDelay: options.keepAliveInitialDelay,<br>      highWaterMark: options.highWaterMark });<br>  if (requestListener) {<br>    this.on(&#39;request&#39;, requestListener);<br>  }<br>  this.httpAllowHalfOpen = false;<br>  this.on(&#39;connection&#39;, connectionListener);<br>  this.on(&#39;listening&#39;, setupConnectionsTracking);<br>  this.timeout = 0;<br>  this.maxHeadersCount = null;<br>  this.maxRequestsPerSocket = 0;<br>  this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);<br>}</pre><p>Notice how the constructor uses clever argument overloading to handle whether we pass a listener function, an option object, or nothing at all.</p><p>However, inside _http_server.js, we spot an unfamiliar module—net:</p><pre>net.Server.call(<br>    this,<br>    { allowHalfOpen: true, noDelay: options.noDelay ?? true,<br>      keepAlive: options.keepAlive,<br>      keepAliveInitialDelay: options.keepAliveInitialDelay,<br>      highWaterMark: options.highWaterMark });</pre><p>This leads us to a key realization: the core TCP foundation of an HTTP server actually originates from the net module. Following this clue, we dig deeper and finally find the ultimate source inside net.js!</p><h3>node/lib/net.js</h3><p>Yo! <a href="https://github.com/nodejs/node/blob/main/lib/net.js">Here</a> is the core Server constructor we&#39;ve been hunting for:</p><pre>function Server(options, connectionListener) {<br>  if (!(this instanceof Server))<br>    return new Server(options, connectionListener);<br><br>EventEmitter.call(this);<br><br>markTransferMode(this, false, true);<br>  if (typeof options === &#39;function&#39;) {<br>    connectionListener = options;<br>    options = kEmptyObject;<br>    this.on(&#39;connection&#39;, connectionListener);<br>  } else if (options == null || typeof options === &#39;object&#39;) {<br>    options = { ...options };<br>    if (typeof connectionListener === &#39;function&#39;) {<br>      this.on(&#39;connection&#39;, connectionListener);<br>    }<br>  } else {<br>    throw new ERR_INVALID_ARG_TYPE(&#39;options&#39;, &#39;Object&#39;, options);<br>  }<br>  if (options.keepAliveInitialDelay !== undefined) {<br>    validateNumber(<br>      options.keepAliveInitialDelay, &#39;options.keepAliveInitialDelay&#39;,<br>    );<br>    if (options.keepAliveInitialDelay &lt; 0) {<br>      options.keepAliveInitialDelay = 0;<br>    }<br>  }<br>  if (options.highWaterMark !== undefined) {<br>    validateNumber(<br>      options.highWaterMark, &#39;options.highWaterMark&#39;,<br>    );<br>    if (options.highWaterMark &lt; 0) {<br>      options.highWaterMark = getDefaultHighWaterMark();<br>    }<br>  }<br>  this._connections = 0;<br>  this[async_id_symbol] = -1;<br>  this._handle = null;<br>  this._usingWorkers = false;<br>  this._workers = [];<br>  this._unref = false;<br>  this._listeningId = 1;<br>  this.allowHalfOpen = options.allowHalfOpen || false;<br>  this.pauseOnConnect = !!options.pauseOnConnect;<br>  this.noDelay = Boolean(options.noDelay);<br>  this.keepAlive = Boolean(options.keepAlive);<br>  this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);<br>  this.highWaterMark = options.highWaterMark ?? getDefaultHighWaterMark();<br>  if (options.blockList) {<br>    if (!module.exports.BlockList.isBlockList(options.blockList)) {<br>      throw new ERR_INVALID_ARG_TYPE(&#39;options.blockList&#39;, &#39;net.BlockList&#39;, options.blockList);<br>    }<br>    this.blockList = options.blockList;<br>  }<br>}</pre><h3>Conclusion</h3><p>What a journey for this Server! We started by calling createServer from the http module, traced our path from node/lib/http.js to node/lib/_http_server.js, and finally arrived at its underlying TCP foundation in node/lib/net.js.</p><p>I hope to share more of my learnings about how Node.js works under the hood soon. Don’t forget to <strong>Follow</strong> and <strong>Subscribe</strong> to my Medium profile so you won’t miss out on the next deep dive. See you next time!</p><p>P.S. If you found this article helpful, feel free to check out more of my technical notes on <a href="https://hackmd.io/@HH7gX6QXRwuPW5_lHiQ7ZA">my HackMD</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88a88cf8638e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>