<?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 Rahul Bhola on Medium]]></title>
        <description><![CDATA[Stories by Rahul Bhola on Medium]]></description>
        <link>https://medium.com/@rahulbhola?source=rss-21e0121a304c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*z0ADiyKbrJc0Caby.jpg</url>
            <title>Stories by Rahul Bhola on Medium</title>
            <link>https://medium.com/@rahulbhola?source=rss-21e0121a304c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:29:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahulbhola/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[Writing beautiful REST Apis]]></title>
            <link>https://medium.com/@rahulbhola/writing-beautiful-rest-apis-8fd42632e04?source=rss-21e0121a304c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8fd42632e04</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[Rahul Bhola]]></dc:creator>
            <pubDate>Fri, 30 Jun 2017 16:02:31 GMT</pubDate>
            <atom:updated>2017-06-30T16:02:31.740Z</atom:updated>
            <content:encoded><![CDATA[<p>Most modern web applications use a common practice of leveraging REST API to make a seperation between data pane and control pane. The API acts as the control pane which is not data intensive but controls the behaviour of application while the static application acts as simple data pane. This seperation makes it possible to extensively cache the user-interface of application for faster response and providing a better user experience. In this article i will be discussing some common practices i have learnt over the years to make an API that is simple to use and yet easy to manage as the size of project grows.</p><h3>1. Embrace stateless authentication</h3><p>This goes without saying but REST APIs need a stateless authentication mechanism. Stateless authentication put in laymens terms is the session data not being stored on server. That means if the client request hits another node of the server it would respond in the same way. The best method i have always prefered for a long time is using <a href="https://jwt.io/">Json-web-token</a> which makes it possible to embed user data right inside the token variable and reduces a database lookup query to get user metadata.</p><p>One thing to remember here is because there is no user session data stored on server it is a good idea to have an expiry time for token. The expiry time can vary according to application needs. A little extra work is required in this case to write <strong>route </strong>to generate a new token based on previous token and refresh token.</p><blockquote>So in order to sum this up, you would need to implement two routes. One for login and other for refresh. Login route returns token and refresh token. On expiry the token and refresh token can be sent back to server in exchange for new token</blockquote><h3>2. Seperate api response meta and data</h3><p>While sending json response from api i always found out that there is some meta data that i cannot pass with standard reponse, like the <em>error Code </em>or <em>status Code . </em>It’s a good practice to differentiate between them. More example of meta content includes the api call limit or simply the expiry time for content.</p><h3>3. Use proper HTTP verbs</h3><p>If you have ever written a REST API, you would get the purpose of this step. But if you have not, there are 4 most common http verbs that are used while designing an api</p><p><strong>GET </strong>Use to simply get a resource from server.</p><p><strong>POST </strong>Use to add new resource to server.</p><p><strong>PUT</strong> Use to update resource on server.</p><p><strong>DELETE</strong> Delete a resource from server.</p><p>Most people should be familiar with <strong>GET</strong> and <strong>POST</strong> . Now the next time you are thinking about writing code to <em>update</em> or <em>delete</em> a resource use proper verbs associated with those.</p><blockquote>Note that DELETE method does not take any body content. You will have to pass resource description in URI for api itself.</blockquote><h3>4. Use of proper status codes</h3><p>There are a lot of important status codes all with different meanings. The list below should help you use the right response for right query.</p><p><strong>200 OK </strong>: Use during a simple GET request. Either fetching a list of resources or a specific resource object.</p><p><strong>201 CREATED</strong> : Use after a POST request when a new resource is created.</p><p><strong>204 NO CONTENT</strong> : Use after DELETE request when a resource no longer exists.</p><p><strong>400 INVALID REQUEST</strong> : The parameters passed over to server are incorrect and server can do nothing to make it right.</p><p><strong>401 UNAUTHORIZED</strong> : Use the status code in case authentication is required to access the resource requested. This can be used with all the verbs.</p><p><strong>403 FORBIDDEN </strong>: Use this if the user is authenticated but does not have the permissions to the requested resource.</p><p><strong>404 NOT FOUND</strong> : Use this response in case the resource does not exist on the server.</p><p><strong>500 INTERNAL SERVER ERROR</strong> : The most common case for this response is when server hits an exception.</p><h3>5. API Version</h3><p>Adding an api version in headers is very important and makes sure that client does not break with changes on api. The same can simply be added using HTTP Header <strong>X-API-VERSION.</strong></p><h3>6. Paginate your API</h3><p>Getting a list of resources from server might be a huge time consuming task and affects the performance of api. Make sure you are adding support for pagination. The details of the same can easily be added in meta section of response for client.</p><h3>7. Use cache to enhance API performance</h3><p>Just like the way you would cache a view remember to cache resource objects also to offload your database servers. I personally prefer using <a href="https://redis.io/">redis</a> but there are a lot of other options out there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8fd42632e04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building angular app using browserify]]></title>
            <link>https://medium.com/hackernoon/building-angular-app-using-browserify-704a7947a09e?source=rss-21e0121a304c------2</link>
            <guid isPermaLink="false">https://medium.com/p/704a7947a09e</guid>
            <category><![CDATA[node]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[browserify]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Rahul Bhola]]></dc:creator>
            <pubDate>Sat, 18 Feb 2017 12:28:00 GMT</pubDate>
            <atom:updated>2019-05-24T06:51:25.448Z</atom:updated>
            <content:encoded><![CDATA[<p>This post is not a <a href="https://hackernoon.com/tagged/tutorial">tutorial</a> on Angular js there are a lot of them online and i would recommend you check them out. This is more about the setup of Angular js so you can build apps that are easy to maintain in the long run and manageable.</p><h3>Start with grunt and browserify</h3><p>If you have worked with something like ember.js you might have noticed that how everything is just in the right folder and it comes out of box with live reload etc. The same is not the case with <a href="https://hackernoon.com/tagged/angular">angular</a> and therefore many people end up doing it wrong. Let us start with creating a package.json to maintain all dependencies</p><pre>npm init --yes</pre><p>Now assuming that you have installed grunt-cli globally here is what you do next. Install packages that you would be needing for live reload and building the apps.</p><pre>npm i --save-dev grunt grunt-browserify grunt-contrib-copy grunt-contrib-watch</pre><p>Obviously if you are developing apps for sometime, you know that scss and jade are your usefull friends. Let us add build tools needed for them also. You can remove this step if you think you want to use html and css.</p><pre>npm i --save grunt-contrib-jade grunt-contrib-sass</pre><p>needless to say if you are using grunt-contrib-sass make sure you have the ruby module for scss installed.</p><h3>Installing angularjs and angular-route</h3><p>In last step we only added dependencied needed for the build, its time to install angular and ngRoute.</p><pre>npm i --save angular angular-route</pre><p>Note that i have not added them in dev-dependencies.</p><h3>Getting folder structure ready</h3><p>Here is a small app structure that i prefer to use to keep my codes manageable</p><pre>.<br>├── app.js<br>├── components<br>│   ├── nav.jade<br>├── controllers<br>│   ├── index.js<br>│   └── testController.js<br>├── factory<br>│   └── index.js<br>├── index.jade<br>├── route.js<br>├── style<br>│   ├── main.scss<br>└── templates<br>    └── application.jade</pre><p>You can always change that to what suits you. This is inside an app folder in my npm project.</p><h3>Building the project</h3><p>Edit Gruntfile.js in your root folder, the one that contains package.json and put in the following content.</p><pre>module.exports = function(grunt){<br>    grunt.initConfig({<br>      pkg: grunt.file.readJSON(&#39;package.json&#39;),<br>      browserify: {<br>        &#39;dist/app.js&#39;: [&#39;app/app.js&#39;]<br>      },<br>      jade :{<br>        compile: {<br>          options: {<br>            client: false,<br>            pretty : false<br>          },<br>          files: [{<br>            cwd: &quot;app&quot;,<br>            src: &quot;**/*.jade&quot;,<br>            dest: &quot;dist&quot;,<br>            expand: &quot;true&quot;,<br>            ext: &quot;.html&quot;<br>          }]<br>        }<br>      },<br>      watch: {<br>        all: {<br>          options: {livereload: true},<br>          files: [&#39;app/**&#39;],<br>          tasks: [&#39;jade&#39;,&#39;sass&#39;,&#39;browserify&#39;]<br>        }<br>      },<br>      sass:{<br>        dist: {<br>          files:{<br>            &#39;dist/main.css&#39;:&#39;app/style/main.scss&#39;<br>          }<br>        }<br>      },<br>      copy: {<br>        main: {<br>          expand: true,<br>          cwd: &#39;static&#39;,<br>          src: &#39;**&#39;,<br>          dest: &#39;dist/static&#39;,<br>        },<br>      }<br>    });</pre><pre>  grunt.loadNpmTasks(&#39;grunt-contrib-jade&#39;);<br>  grunt.loadNpmTasks(&#39;grunt-contrib-sass&#39;);<br>  grunt.loadNpmTasks(&#39;grunt-contrib-watch&#39;);<br>  grunt.loadNpmTasks(&#39;grunt-browserify&#39;);<br>  grunt.loadNpmTasks(&#39;grunt-contrib-copy&#39;);<br>  <br>  grunt.registerTask(&#39;default&#39;, [&#39;copy&#39;,&#39;jade&#39;,&#39;sass&#39;,&#39;browserify&#39;,&#39;watch&#39;]);<br>};</pre><p>That is all that we need for building our app, which grunt will build and put in dist folder.</p><h3>Put some actual content in angular app</h3><p>I am just going to make a simple app that imports nav.</p><pre>&lt;!--app/index.jade--&gt;<br>doctype<br>html.no-js<br>  head<br>    meta(charset=&#39;utf-8&#39;)<br>    meta(http-equiv=&#39;X-UA-Compatible&#39;, content=&#39;IE=edge&#39;)<br>    base(href=&#39;/&#39;)<br>    title Application<br>    meta(name=&#39;description&#39;, content=&#39;&#39;)<br>    meta(name=&#39;viewport&#39;, content=&#39;width=device-width, initial-scale=1&#39;)<br>    link(rel=&#39;stylesheet&#39;,href=&#39;main.css&#39;)<br>  body(ng-app=&#39;application&#39;)<br>    div(ng-view)<br>    script(src=&#39;app.js&#39;)<br>    script(src=&#39;//localhost:35729/livereload.js&#39;)</pre><p>The last script of livereload comes from grunt-watch and would reload the page when you change anything in your code.</p><pre>// app/app.js<br>var angular = require(&#39;angular&#39;);<br>var ngRoute = require(&#39;angular-route&#39;);<br>var app = angular.module(&#39;application&#39;, [ngRoute]);</pre><pre>require(&#39;./route&#39;)(app);<br>require(&#39;./controllers&#39;);</pre><p>Remember we already have angular and angular-route installed.</p><pre>// app/routes.js<br>module.exports = function(app) {<br>  app.config(function($routeProvider, $locationProvider) {<br>    $routeProvider<br>      .when(&quot;/&quot;,{<br>        templateUrl: &quot;templates/application.html&quot;<br>      });<br>    $locationProvider.html5Mode(true);<br>  });<br>};</pre><p>This is the file where we define all the routes. Note that templates/application.html is the output of jade from templates/application.jade.</p><pre>// app/controllers/index.js<br>var app = require(&#39;angular&#39;).module(&#39;application&#39;);</pre><pre>app.controller(&#39;testController&#39;,require(&#39;./testController.js&#39;));</pre><p>This is the file that was imported in app/app.js, we need to include all controllers here. In this case i have demonstrated with testController as you can see from the tree structure.</p><h3>What about components ?</h3><p>that is more of common reusable stuff like navigation that i keep so my templates directory don’t become cluttered. Example in application.jade which in this case is the landing page, i can add navigation using</p><pre>div(ng-include=&quot;&#39;/components/nav.html&#39;&quot;)</pre><h3>How to see the changes in browser</h3><p>Well first you need to start the build, Simply go to root folder of npm project and run grunt from terminal. It will build the project and start to look for changes.</p><p>You might also see a new directory called dist. That directory is the final build output for the code. Simply start a web-server in that directory and go to that url in browser, where you can see the application running.</p><blockquote><em>The moment you make any changes to the code, grunt would automatically run and the browser would reflect the updated changes without the need to reload.</em></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=704a7947a09e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/building-angular-app-using-browserify-704a7947a09e">Building angular app using browserify</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hosting a private npm server]]></title>
            <link>https://medium.com/@rahulbhola/hosting-a-private-npm-server-e8c254c4ec11?source=rss-21e0121a304c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8c254c4ec11</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Rahul Bhola]]></dc:creator>
            <pubDate>Tue, 14 Feb 2017 06:24:51 GMT</pubDate>
            <atom:updated>2017-02-14T06:24:51.494Z</atom:updated>
            <content:encoded><![CDATA[<p>NPM is awesome but i hate the fact that i have to download the same package again for all projects. Plus for hosting private servers i need a paid account. I realize that they need to keep there servers going and therefore.. Anyhow this is a pretty short post on how you can set up your custom npm server.</p><h3>Installing packages</h3><p><a href="https://www.npmjs.com/package/sinopia">Sinopia</a> is a pretty nice alternative. You can host it yourself and at the same time it’s cache saves me a lot of time installing common packages. Install it simply using</p><pre>npm i -g sinopia</pre><p>and you are pretty much done. All that is left is that you start the process and keep it running. Let’s use <a href="https://www.npmjs.com/package/pm2">pm2</a> for that. Follow the following steps</p><pre>npm i -g pm2<br>pm2 start sinopia</pre><p>And as you might have guessed we have a custom npm server of our own running.</p><blockquote><em>If sinopia doesn’t find packages locally it would query </em><a href="https://npmjs.com/"><em>npm</em></a><em> for them so no worries over there.</em></blockquote><h3>Configuring npm</h3><p>Now all that we have left is setting up npm configuration use our local server. By default sinopia runs on port <strong>4378</strong>. Simply add that to the registry using</p><pre>npm set registry <a href="http://localhost:4873/">http://localhost:4873/</a></pre><p>And you are all done. You can now publish private packages as well as maintain a local cache. Hope this helps you guys out.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8c254c4ec11" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>