<?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: Dodo Mukunzi</title>
    <description>The latest articles on DEV Community by Dodo Mukunzi (@mukunzidd).</description>
    <link>https://dev.to/mukunzidd</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%2F130678%2F0df7a31d-7152-439d-9c59-fdb9e2a1e837.jpeg</url>
      <title>DEV Community: Dodo Mukunzi</title>
      <link>https://dev.to/mukunzidd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mukunzidd"/>
    <language>en</language>
    <item>
      <title>Naming API Routes: Best Practices to Make Your API Intuitive</title>
      <dc:creator>Dodo Mukunzi</dc:creator>
      <pubDate>Fri, 10 Mar 2023 11:38:40 +0000</pubDate>
      <link>https://dev.to/mukunzidd/naming-api-routes-best-practices-to-make-your-api-intuitive-2fcc</link>
      <guid>https://dev.to/mukunzidd/naming-api-routes-best-practices-to-make-your-api-intuitive-2fcc</guid>
      <description>&lt;p&gt;When it comes to building a RESTful API, naming your routes is a critical part of creating a well-designed and easily understood API. In this article, we will explore the best practices for naming RESTful API routes, and look at some common ways people can mess up their API route names. We will also use emojis to make our examples more fun and memorable.&lt;/p&gt;

&lt;p&gt;🔑 Best Practices for Naming API Routes&lt;/p&gt;

&lt;p&gt;👉 1️⃣ &lt;strong&gt;Use Nouns to Represent Resources&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most important principles of RESTful API design is to use nouns to represent resources. This means that your routes should describe the things that your API is responsible for managing.&lt;/p&gt;

&lt;p&gt;👍 Good:&lt;br&gt;
GET &lt;code&gt;/users&lt;/code&gt;: Retrieve a list of users&lt;br&gt;
POST &lt;code&gt;/users&lt;/code&gt;: Create a new user&lt;br&gt;
GET &lt;code&gt;/users/:id&lt;/code&gt;: Retrieve a specific user&lt;/p&gt;

&lt;p&gt;👎 Bad:&lt;br&gt;
GET /get-users: ❌ This route includes a verb that is unnecessary and doesn't describe the resource being managed.&lt;/p&gt;

&lt;p&gt;👉 2️⃣ &lt;strong&gt;Use Plural Form for Collections and Singular Form for Single Resources&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When dealing with collections, it's best to use the plural form for the route. When dealing with a single resource, it's best to use the singular form.&lt;/p&gt;

&lt;p&gt;👍 Good:&lt;br&gt;
GET &lt;code&gt;/users&lt;/code&gt;: Retrieve a list of users&lt;br&gt;
GET &lt;code&gt;/users/:id&lt;/code&gt;: Retrieve a specific user&lt;/p&gt;

&lt;p&gt;👎 Bad:&lt;br&gt;
GET &lt;code&gt;/user&lt;/code&gt;: ❌ This route doesn't indicate whether it's for a single user or a collection of users.&lt;/p&gt;

&lt;p&gt;👉 3️⃣ &lt;strong&gt;Use HTTP Methods for Actions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;HTTP methods are the primary way to describe what action you want to perform on a resource. You should use them whenever possible, as it makes your API more intuitive to use.&lt;/p&gt;

&lt;p&gt;👍 Good:&lt;br&gt;
GET &lt;code&gt;/users&lt;/code&gt;: Retrieve a list of users&lt;br&gt;
POST &lt;code&gt;/users&lt;/code&gt;: Create a new user&lt;br&gt;
PUT &lt;code&gt;/users/:id&lt;/code&gt;: Update a specific user&lt;br&gt;
DELETE &lt;code&gt;/users/:id&lt;/code&gt;: Delete a specific user&lt;/p&gt;

&lt;p&gt;👎 Bad:&lt;br&gt;
GET &lt;code&gt;/create-user&lt;/code&gt;: ❌ This route includes a verb that describes the action being performed, which should be indicated by the HTTP method.&lt;/p&gt;

&lt;p&gt;👉 4️⃣ &lt;strong&gt;Use Nouns or Adjectives in the Path for Filters or Sorting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, you need to filter or sort your data. In these cases, you should use nouns or adjectives in the path to indicate what you're filtering or sorting by.&lt;/p&gt;

&lt;p&gt;👍 Good:&lt;br&gt;
GET &lt;code&gt;/users?active=true&lt;/code&gt;: Retrieve a list of active users&lt;br&gt;
GET &lt;code&gt;/users?sort=name&lt;/code&gt;: Retrieve a list of users sorted by name&lt;/p&gt;

&lt;p&gt;👎 Bad:&lt;br&gt;
GET &lt;code&gt;/users/sortByName&lt;/code&gt;: ❌ This route includes a verb that describes the action being performed, which should be indicated by the HTTP method.&lt;/p&gt;

&lt;p&gt;👉 5️⃣ &lt;strong&gt;Use Hyphens to Separate Words in the URL Path&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's a best practice to use hyphens to separate words in your URL path. This makes your URLs more readable and easier to understand.&lt;/p&gt;

&lt;p&gt;👍 Good:&lt;br&gt;
GET &lt;code&gt;/user-profile&lt;/code&gt;: Retrieve a user profile&lt;br&gt;
POST &lt;code&gt;/post-comment&lt;/code&gt;: Create a new comment on a post&lt;/p&gt;

&lt;p&gt;👎 Bad:&lt;br&gt;
GET &lt;code&gt;/user_profile&lt;/code&gt;: ❌ This route uses underscores instead of hyphens, which can make it harder to read and understand.&lt;/p&gt;

&lt;p&gt;In brief, following these five best practices for naming API routes can greatly improve the clarity and intuitiveness of your API design. By using nouns to represent resources, using HTTP methods for actions, and using hyphens to separate words in the URL path, you can make your API easier to understand and use for both developers and end-users. Conversely, violating these best practices can make your API confusing and difficult to work with.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>apirouting</category>
    </item>
    <item>
      <title>Dynamic RESTful API Design with HATEOAS 🚀</title>
      <dc:creator>Dodo Mukunzi</dc:creator>
      <pubDate>Thu, 02 Feb 2023 17:42:24 +0000</pubDate>
      <link>https://dev.to/mukunzidd/dynamic-restful-api-design-with-hateoas-46k8</link>
      <guid>https://dev.to/mukunzidd/dynamic-restful-api-design-with-hateoas-46k8</guid>
      <description>&lt;p&gt;Are you tired of constantly having to update your API every time you make changes to your server-side code? 💩 HATEOAS (Hypermedia as the Engine of Application State) is here to save the day!&lt;/p&gt;

&lt;p&gt;In this article, we'll dive into what HATEOAS is all about, why it's crucial for RESTful API design, and how to implement it in a Node.js/Express application 🚀.&lt;/p&gt;

&lt;p&gt;What is HATEOAS, you ask? 🤔&lt;/p&gt;

&lt;p&gt;HATEOAS is a key principle of RESTful architecture that ensures a client interacts with a server-driven application through hypermedia links within resource representations. This means that the client receives a resource representation that includes links to related resources. The client can then dynamically navigate the application's state using these links, without having to hard-code a fixed set of interactions.&lt;/p&gt;

&lt;p&gt;Why is HATEOAS Important for RESTful API Design? 🤔&lt;/p&gt;

&lt;p&gt;By using HATEOAS, APIs become more flexible, scalable, and maintainable. The client doesn't have to know about all the possible interactions in advance. Instead, it can discover available resources and interact with them dynamically through the hypermedia links provided by the server. This makes updates to the API much easier, as the server can add or remove links without affecting the client.&lt;/p&gt;

&lt;p&gt;How to Implement HATEOAS in a Node.js/Express Application 💻&lt;/p&gt;

&lt;p&gt;Let's take a look at a simple book library API as an example. First, we'll create a book resource representation that includes hypermedia links. Here's an example of what this could look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"To Kill a Mockingbird"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Harper Lee"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"links"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"rel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"self"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/books/1"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"rel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"collection"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/books"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the "self" link points to the current book resource, and the "collection" link points to the collection of all book resources.&lt;/p&gt;

&lt;p&gt;Next, we'll create a &lt;code&gt;GET /books/:id&lt;/code&gt; endpoint to retrieve a book resource:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/books/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The book with the given ID was not found.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="s2"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! By including hypermedia links within resource representations, clients can dynamically discover available resources and interact with them.&lt;/p&gt;

&lt;p&gt;In conclusion, HATEOAS is the missing piece for making RESTful APIs truly dynamic and user-friendly. So, go ahead and add some 🔗 to your API resources – it'll make things easier for your clients and keep your API up-to-date. &lt;/p&gt;

&lt;p&gt;As always, happy coding! 💻&lt;/p&gt;

</description>
      <category>restful</category>
      <category>api</category>
      <category>hateoas</category>
      <category>express</category>
    </item>
    <item>
      <title>View All HTTP Status Codes Offline 😜</title>
      <dc:creator>Dodo Mukunzi</dc:creator>
      <pubDate>Fri, 06 Sep 2019 09:28:42 +0000</pubDate>
      <link>https://dev.to/mukunzidd/view-all-http-status-codes-offline-4984</link>
      <guid>https://dev.to/mukunzidd/view-all-http-status-codes-offline-4984</guid>
      <description>&lt;p&gt;If you have NodeJS installed just type &lt;code&gt;node&lt;/code&gt; in your command line/shell/terminal then type &lt;code&gt;http.STATUS_CODES&lt;/code&gt;. All status codes will be returned, just like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;node
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; http.STATUS_CODES
&lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'100'&lt;/span&gt;: &lt;span class="s1"&gt;'Continue'&lt;/span&gt;,
  &lt;span class="s1"&gt;'101'&lt;/span&gt;: &lt;span class="s1"&gt;'Switching Protocols'&lt;/span&gt;,
  &lt;span class="s1"&gt;'102'&lt;/span&gt;: &lt;span class="s1"&gt;'Processing'&lt;/span&gt;,
  &lt;span class="s1"&gt;'200'&lt;/span&gt;: &lt;span class="s1"&gt;'OK'&lt;/span&gt;,
  &lt;span class="s1"&gt;'201'&lt;/span&gt;: &lt;span class="s1"&gt;'Created'&lt;/span&gt;,
  &lt;span class="s1"&gt;'202'&lt;/span&gt;: &lt;span class="s1"&gt;'Accepted'&lt;/span&gt;,
  ... &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also try &lt;code&gt;http.METHODS&lt;/code&gt; just for more fun.&lt;/p&gt;

</description>
      <category>http</category>
      <category>statuscodes</category>
    </item>
    <item>
      <title>Top 10 Commonly Used HTTP Status Codes</title>
      <dc:creator>Dodo Mukunzi</dc:creator>
      <pubDate>Fri, 19 Jul 2019 09:28:09 +0000</pubDate>
      <link>https://dev.to/mukunzidd/10-most-used-http-status-codes-2ck5</link>
      <guid>https://dev.to/mukunzidd/10-most-used-http-status-codes-2ck5</guid>
      <description>&lt;p&gt;HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped into five classes: informational responses, successful responses, redirects, client errors, and server errors. &lt;/p&gt;

&lt;p&gt;Here are the 10 commonly used and useful HTTP status codes:&lt;/p&gt;

&lt;h2&gt;
  
  
  1xx: Informational
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;None in this class.&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2xx: Success
&lt;/h2&gt;

&lt;h4&gt;
  
  
  200 OK
&lt;/h4&gt;

&lt;p&gt;It is the general status code. Most common code used to indicate success.&lt;br&gt;
The request has succeeded. The meaning of success varies depending on the HTTP method:&lt;br&gt;
&lt;strong&gt;GET:&lt;/strong&gt; The resource has been fetched and is transmitted in the message body.&lt;br&gt;
&lt;strong&gt;HEAD:&lt;/strong&gt; The entity headers are in the message body.&lt;br&gt;
&lt;strong&gt;PUT or POST:&lt;/strong&gt; The resource describing the result of the action is transmitted in the message body.&lt;br&gt;
&lt;strong&gt;TRACE:&lt;/strong&gt; The message body contains the request message as received by the server&lt;/p&gt;

&lt;h4&gt;
  
  
  201 Created
&lt;/h4&gt;

&lt;p&gt;Successful creation occurred (via either POST or PUT). Set the Location header to contain a link to the newly-created resource (on POST). Response body content may or may not be present.&lt;/p&gt;

&lt;h4&gt;
  
  
  204 No Content
&lt;/h4&gt;

&lt;p&gt;Status when wrapped responses are not used and nothing is in the body (e.g. DELETE).&lt;/p&gt;

&lt;h2&gt;
  
  
  3xx: Redirection
&lt;/h2&gt;

&lt;h4&gt;
  
  
  304 Not Modified
&lt;/h4&gt;

&lt;p&gt;This is used for caching purposes. It tells the client that the response has not been modified, so the client can continue to use the same cached version of the response.&lt;/p&gt;

&lt;h2&gt;
  
  
  4xx: Client Error
&lt;/h2&gt;

&lt;h4&gt;
  
  
  400 Bad Request
&lt;/h4&gt;

&lt;p&gt;General error when fulfilling the request would cause an invalid state. Domain validation errors, missing data, etc. are some examples.&lt;/p&gt;

&lt;h4&gt;
  
  
  401 Unauthorized
&lt;/h4&gt;

&lt;p&gt;The error code response for missing or invalid authentication token.&lt;/p&gt;

&lt;h4&gt;
  
  
  403 Forbidden
&lt;/h4&gt;

&lt;p&gt;The error code for a user not authorized to perform the operation or the resource is unavailable for some reason (e.g. time constraints, etc.).&lt;/p&gt;

&lt;h4&gt;
  
  
  404 Not Found
&lt;/h4&gt;

&lt;p&gt;Used when the requested resource is not found, whether it doesn't exist or if there was a 401 or 403 that, for security reasons, the service wants to mask.&lt;/p&gt;

&lt;h4&gt;
  
  
  409 Conflicts
&lt;/h4&gt;

&lt;p&gt;Whenever a resource conflict would be caused by fulfilling the request. Duplicate entries and deleting root objects when cascade-delete is not supported are a couple of examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  5xx: Server Error
&lt;/h2&gt;

&lt;h4&gt;
  
  
  500 Internal Server Error
&lt;/h4&gt;

&lt;p&gt;The general catch-all error when the server-side throws an exception.&lt;/p&gt;

</description>
      <category>http</category>
      <category>statuscodes</category>
      <category>api</category>
      <category>rest</category>
    </item>
  </channel>
</rss>
