<?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 Swarup A. Kharul on Medium]]></title>
        <description><![CDATA[Stories by Swarup A. Kharul on Medium]]></description>
        <link>https://medium.com/@swarupkharulsk?source=rss-eee055c4470c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BJpGhWaYQsvOIE6Uf5v5vA.jpeg</url>
            <title>Stories by Swarup A. Kharul on Medium</title>
            <link>https://medium.com/@swarupkharulsk?source=rss-eee055c4470c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 06:01:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swarupkharulsk/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[Restful CRUD API with hapi.js]]></title>
            <link>https://medium.com/acmvit/restful-crud-api-with-hapi-js-c92edd36d8df?source=rss-eee055c4470c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c92edd36d8df</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[hapijs]]></category>
            <dc:creator><![CDATA[Swarup A. Kharul]]></dc:creator>
            <pubDate>Sat, 17 Jul 2021 11:28:33 GMT</pubDate>
            <atom:updated>2021-07-24T03:47:32.910Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*MnDxY0RGDHbowSO3WaTQGQ.png" /></figure><h3>Introduction</h3><p><a href="https://hapi.dev/">Hapi.js</a> (shorthand for Http-API, pronounced <em>happy</em> and also known as <em>hapi</em>) is an open-source Node.js framework used to build powerful and scalable web applications. Hapi is commonly used to build Application Programming Interface (API) servers, HTTP-proxy applications, and websites. Hapi.js was created by a team at Walmart Labs to handle their traffic for events like Black Friday, which is by far one of the busiest days for online shopping on the U.S. calendar. Hapi was originally built using the express framework before facing challenges that drove Walmart to make hapi, its own stand-alone framework.</p><h4>What is CRUD?</h4><p><a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> is an acronym for Create, Read, Update and Delete. It is a set of operations we get servers to execute (POST, GET, PUT and DELETE requests respectively). This is what each operation does:</p><ul><li>Create (POST) — Make something</li><li>Read (GET)- Get something</li><li>Update (PUT) — Change/Edit something</li><li>Delete (DELETE)- Remove something</li></ul><p>POST, GET, PUT, and DELETE requests let us construct <a href="https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/">Rest APIs</a>.</p><p>Great! Let’s move on.</p><h3>Prerequisites</h3><p>Before building your API make sure you have <a href="https://nodejs.org/en/">Node.js</a> and <a href="https://www.mongodb.com/">MongoDB </a>installed and setup on your machine before starting this tutorial. The complete code can be found <a href="https://github.com/SwarupKharul/CRUD_App_Hapi/">here</a>.</p><h3>Getting Started</h3><p>The first step in any new project is setting up the directory. So, fire up your terminal and create a new directory for the application.</p><pre>mkdir CRUD_App<br>cd CRUD_App</pre><p>Initialize the application with a package.json file</p><pre>npm init</pre><p>Install Hapi</p><pre>npm install @hapi/hapi</pre><h3>Creating a simple Server</h3><p>A very basic hapi server looks like the following and displays “Hello World!” in your browser.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/998eaf39d658eb54061bec0cfc393ac7/href">https://medium.com/media/998eaf39d658eb54061bec0cfc393ac7/href</a></iframe><p>Save the above as index.js and start the server with the command node index.js. Now visit http://localhost:3000 in your browser, and voila!! You have setup your own server.</p><h4>Database connection using Mongoose ODM</h4><p>Install Mongoose ODM</p><pre>npm install mongoose</pre><p>Add this code in the beginning to connect to mongodb using the mongoose ODM.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edfd98707869f83806c3e5af0f5497ab/href">https://medium.com/media/edfd98707869f83806c3e5af0f5497ab/href</a></iframe><p>Great! Now that we have learnt the basics it’s time to start with the CRUD operations.</p><p>We will be making a simple CRUD Restful API for a Notes App.</p><h4>Creating the schema</h4><p>The schema defines the structure of our database. Schema helps to organize the documents in the collection. Since we are creating a Note, our schema should contain the following fields</p><ul><li>title</li><li>important</li><li>description</li></ul><p>The schema for the data is created using the mongoose.model method. The type of data should be specified.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67aa97c81a921b32494c0e19b3748e92/href">https://medium.com/media/67aa97c81a921b32494c0e19b3748e92/href</a></iframe><h4>Create a Note</h4><p>To create a Note we send a post request which contains the data. request.payloadcontains the data that we pass from the frontend. We create a new instance of the Note model we created and saves it to DB using the collection.save method. Then the response is returned.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cada90ea14902fc5ffe1873e5ce2da2/href">https://medium.com/media/7cada90ea14902fc5ffe1873e5ce2da2/href</a></iframe><h4>Get all Notes with filter</h4><p>To get a list of all the notes we send a get request. We can also pass parameters to get a filtered list. For example &#39;/api/notes?important=true’ will give a list of all the important notes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f68cbf4985d370f8ac985c909976c96f/href">https://medium.com/media/f68cbf4985d370f8ac985c909976c96f/href</a></iframe><h4>Update a Note</h4><p>For updating the Note, we will use the object id of the Note. We pass the object id of the Note as a parameter of the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13d10ccfdf5261d6399b0ab32d73bca4/href">https://medium.com/media/13d10ccfdf5261d6399b0ab32d73bca4/href</a></iframe><h4>Delete a Note</h4><p>Deleting the Note is also done using the object id of the Note. For deleting the Note, we pass the object id of the Note as a parameter of the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8a92cfef30075c2d119c431919f060d/href">https://medium.com/media/d8a92cfef30075c2d119c431919f060d/href</a></iframe><h3>Final Thoughts:</h3><p>Now that your API is all ready to go you can test it out yourself using <a href="https://www.postman.com/">Postman</a> with the URL <a href="http://localhost:5000/api/posts">http://localhost:3000/api</a>. The complete code for this blog can be found <a href="https://github.com/SwarupKharul/CRUD_App_Hapi/">here</a>.</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c92edd36d8df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acmvit/restful-crud-api-with-hapi-js-c92edd36d8df">Restful CRUD API with hapi.js</a> was originally published in <a href="https://medium.com/acmvit">ACM VIT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>