<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marlon Yepes Ceballos</title>
    <description>The latest articles on DEV Community by Marlon Yepes Ceballos (@myepes82).</description>
    <link>https://dev.to/myepes82</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1181751%2Fe43a22c9-8a19-4976-bd62-7e886332ac93.jpeg</url>
      <title>DEV Community: Marlon Yepes Ceballos</title>
      <link>https://dev.to/myepes82</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/myepes82"/>
    <language>en</language>
    <item>
      <title>!!BUN? How to Develop an HTTP Server and Handle Routes Dynamically ✔💻🗜</title>
      <dc:creator>Marlon Yepes Ceballos</dc:creator>
      <pubDate>Tue, 17 Oct 2023 21:39:50 +0000</pubDate>
      <link>https://dev.to/myepes82/bun-how-to-develop-an-http-server-and-handle-routes-dynamically-125k</link>
      <guid>https://dev.to/myepes82/bun-how-to-develop-an-http-server-and-handle-routes-dynamically-125k</guid>
      <description>&lt;p&gt;&lt;strong&gt;JavaScript interpeters...&lt;/strong&gt;&lt;br&gt;
In recent years, we have witnessed significant evolution in JavaScript interpreters on the server side. One after another, they have emerged, each bringing improvements and unique qualities compared to its predecessor. Each of them is striving for something different: performance, ease of coding, scalability, and more. Among the various newcomers, there is Bun. This new yet formidable competitor has made tremendous strides in terms of development speed and operational performance 😬.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why bun? 🤔&lt;/strong&gt;&lt;br&gt;
To describe Bun, we could use various terms, although the most appropriate one might be 'framework.' Bun is conceived as an all-in-one toolkit; this 'toolkit,' as its creators call it, is a bit broader than it might seem. Bun offers much more than other interpreters could or dare to. It functions as a package manager, a testing environment, a runtime environment, and a bundler (Webpack? Not needed with Bun). This, coupled with execution speeds that seem instantaneous and package installations even 10X (or more) faster compared to other dependency managers, puts Bun on the radar of many developers today.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other features that make Bun unique include 🌟:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Backward compatibility with Node.js: Bun allows for easy migration of existing projects created under Node.js, ensuring interoperability between versions and, at the same time, promising to enhance existing speeds ⏩.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enables code writing and compatibility with TS, JSX, TSX, JS files.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Bun is more than a runtime. The long-term goal is to be a cohesive, infrastructural toolkit for building apps with JavaScript/TypeScript, including a package manager, transpiler, bundler, script runner, test runner, and more.&lt;br&gt;&lt;br&gt;
&lt;a href="https://bun.sh/docs"&gt;Bun/Docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;EX-&lt;/strong&gt;&lt;br&gt;
With this background, let's get down to the nitty-gritty: how to use it, how to achieve tasks we used to do with other tools, starting with the most common one—setting up a web server (something akin to Express and Node.js). Although there are already frameworks to 'simplify' the process even further with Bun, we will explore how to do it natively, easily, and succinctly with what Bun provides.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Step&lt;/strong&gt;&lt;br&gt;
Before we dive into Bun, we need to make sure it's installed. There are several ways to install it, and here's how:&lt;br&gt;
Linux/MacOs: &lt;br&gt;
&lt;code&gt;curl -fsSL https://bun.sh/install | bash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Homebrew: &lt;br&gt;
&lt;code&gt;brew tap oven-sh/bun&lt;/code&gt;&lt;br&gt;
&lt;code&gt;brew install bun&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once installed, we will kick off our project as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RmT_fHbZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blrqy3gulrgrw95deunt.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RmT_fHbZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blrqy3gulrgrw95deunt.PNG" alt="Bun project initialization" width="800" height="221"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Server initialization&lt;/strong&gt;&lt;br&gt;
To initialize our HTTP server with Bun, we can do so by writing the code directly in our index.ts file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P2AZfYFO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7g64kd7vvv096kvdcp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P2AZfYFO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7g64kd7vvv096kvdcp5.png" alt="Intex.ts" width="800" height="571"&gt;&lt;/a&gt;&lt;br&gt;
This is the basic way to set up our native server in Bun. In this case, any request reaching the server's base path will receive the same response: 'Example response.' But what if we want to handle more requests? More paths?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--05zmzXHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgtf1rhtmvqdcj0jdq0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--05zmzXHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgtf1rhtmvqdcj0jdq0d.png" alt="Multi routing handling" width="800" height="462"&gt;&lt;/a&gt;&lt;br&gt;
This looks a bit better, but is it the best way? Of course not. There isn't a specific way that is the best, but we can certainly strive for it. Here's a new approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All routes should have at least a path and a handler. Knowing this, we can create an interface to represent our APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QBb1PnGP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqw1rfnmfo7bty8vouua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QBb1PnGP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqw1rfnmfo7bty8vouua.png" alt="RequestsGroup" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With our schema in place, we can generate a handler function capable of representing a switch between the various APIs we want to expose, something like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1qI77cE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyx52ml85fetz584iju1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1qI77cE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyx52ml85fetz584iju1.png" alt="Handler" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With this function, we can store our routes dynamically, allowing us to grow in terms of routes and APIs as much as we want to implement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to use our handler function?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eSkQX-lV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ya2oqqv5au28rd84qco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eSkQX-lV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ya2oqqv5au28rd84qco.png" alt="HAndler use" width="800" height="519"&gt;&lt;/a&gt;&lt;br&gt;
With this, it's enough to utilize our handler function, opening the door to various implementations. This solution is just one of many, with possible shortcomings and improvements, such as replacing the recurring array search with a switch statement. Nevertheless, it's just an approach to the flexibility and possibilities that Bun opens up for us as developers. Finally, to run our Bun server, it will be sufficient to execute the command [bun run ./src/index.ts]. Below, you'll find the link to the project on GitHub with the project structure and various implementations for our handler function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/myepes82/bun_http_server"&gt;[GitHub] &lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bunjs</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>Error handling in Express Server - NodeJS</title>
      <dc:creator>Marlon Yepes Ceballos</dc:creator>
      <pubDate>Tue, 10 Oct 2023 21:25:11 +0000</pubDate>
      <link>https://dev.to/myepes82/error-handling-in-express-server-nodejs-44c</link>
      <guid>https://dev.to/myepes82/error-handling-in-express-server-nodejs-44c</guid>
      <description>&lt;p&gt;When you want to develop a backend app using Node.js and Express as frameworks for server building, you must have a way to handle the errors that could occur during a service execution.&lt;br&gt;
One way to prevent unhandled errors or non-execution responses is by using the middleware functions that Express allows us to use. Here is an example of how to catch and return a server response:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As a good practice normalize ur server response structures: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JXcOYhMO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n6zjry1grto4xtfl5vv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JXcOYhMO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n6zjry1grto4xtfl5vv.png" alt="Common Http Server Response Interface" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--euSngscc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7lqh718yptg30553p8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--euSngscc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7lqh718yptg30553p8e.png" alt="Error Http Server Response Interface" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once u have created interfaces for define ur server response object u have to create classes thath extends from Error native class for representing possible server errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CaQtgpvn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txs3sgxw47ph7u06x3ls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CaQtgpvn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txs3sgxw47ph7u06x3ls.png" alt="Common Http Error Exception Class" width="800" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tuaJHMDZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lazt9nlwris93aziy2c2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tuaJHMDZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lazt9nlwris93aziy2c2.png" alt="ResourceNotFound Exception Class" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All errors in Node.js extend from the Error class. This allows you to create a custom error class, extend from it, and use reserved keys like 'throw' for throwing your custom errors. Now, you have to create the middleware function to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T8DfP61P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zza6432nxnlgnid9d5oa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T8DfP61P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zza6432nxnlgnid9d5oa.png" alt="Http Error Middleware" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once you have your middleware function defined it's time to pass it to express &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H-Am_Aur--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ee191wmp9n7tss2inebj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H-Am_Aur--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ee191wmp9n7tss2inebj.png" alt="Middleware use setting" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To handle errors in your server application, you need to use the 'next' function from Express, as demonstrated below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--64nfVYgl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkb4ydfz5lh4cdkvkodi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--64nfVYgl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkb4ydfz5lh4cdkvkodi.png" alt="Http Error Middleware use" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example response:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RlHbAcfm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5pxgzfrixja1ox4vukj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RlHbAcfm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5pxgzfrixja1ox4vukj.PNG" alt="Image description" width="800" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is just a way to handle possible server custom errors, this example were made with Typescript.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
