<?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: Mayank Shah</title>
    <description>The latest articles on DEV Community by Mayank Shah (@mayankshah1607).</description>
    <link>https://dev.to/mayankshah1607</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%2F165330%2Fd2320409-4b06-4739-b9ee-988c5f1000a8.JPG</url>
      <title>DEV Community: Mayank Shah</title>
      <link>https://dev.to/mayankshah1607</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mayankshah1607"/>
    <language>en</language>
    <item>
      <title>Automatically generating boiler plate code for NodeJS REST APIs</title>
      <dc:creator>Mayank Shah</dc:creator>
      <pubDate>Mon, 13 May 2019 11:08:34 +0000</pubDate>
      <link>https://dev.to/mayankshah1607/generating-boiler-plate-code-for-nodejs-rest-apis-fpb</link>
      <guid>https://dev.to/mayankshah1607/generating-boiler-plate-code-for-nodejs-rest-apis-fpb</guid>
      <description>&lt;p&gt;I have been a NodeJS developer for almost about a year now. I mostly develop REST APIs with mongoose and express. Initially when I began developing, my code was not considered "clean". I was later introduced to the concept of MVC, and how my project must be structured accordingly. I mostly organised my code into 3 folders - models, controllers and routes. 'Models' would consist of mongoose models with defined schema. Controllers provided the main functionality between these 'models' and incoming requests from the client-side or 'views'. I found myself writing a lot of boiler-plate code to make sure that my applications follow such an architecture.&lt;/p&gt;

&lt;p&gt;This is when I developed an npm module for personal use - "node-boiler", which I later made open source (You can find it &lt;a href="https://github.com/mayankshah1607/node-boiler"&gt;here&lt;/a&gt;). It is very easy to use. You just need to setup your project configuration in a file called "boil.yml". Save this file in the root directory of the project, and run the command "nodeboil". Your entire project with all the directories and files with boiler plate code will be generated! :)&lt;/p&gt;

&lt;p&gt;There's a lot of project templates out there which you can use as boiler plate. But, 'node-boiler' offers great flexibility, ensuring that your project is configured just as per your needs. You get to choose what you want, and how you want it. Moreover, it is a CLI, which means, you don't even have to include any code from this module in your code base.&lt;/p&gt;

&lt;p&gt;Let's give this a try.&lt;/p&gt;

&lt;p&gt;I must first install this library globally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i -g node-boiler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once, that's done, I need to create a file called &lt;code&gt;boil.yml&lt;/code&gt; and save it in the root of the project. The &lt;code&gt;boil.yml&lt;/code&gt; is self-explanatory. Consider this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;models:
  - 'users'
  - 'admins'
  - 'players'

controllers:
  authController:
    - 'login'
    - 'signUp'

  playerController:
    - 'pass'
    - 'shoot'

views:
  - 'home'
  - 'profile' 

routes:
  admin-routes:
    post:
      - '/delete'
      - '/another-route'
    get:
      - '/get-here'
      - '/lol'
  player-routes:
    get:
      - '/shoot'
      - '/kick'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It defines your project structure in the most obvious way.&lt;/p&gt;

&lt;p&gt;Once this is done, run the following command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nodeboil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're likely to see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generated file users.js
Generated file admins.js
Generated file players.js
Generated file authController.js
Generated file playerController.js
Generated file home.html
Generated file profile.html
Generated file admin-routes.js
Generated file player-routes.js
*******GENERATED ALL FILES*******
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And your project directory will now look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; --node_modules
   |--your modules
 --models
   |--users.js
   |--admins.js
   |--players.js
 --views
   |--home.html
   |--profile.html
 --controllers
   |--authController.js
   |--playerController.js
 --routes
   |--admin-routes.js
   |--player-routes.js
 --boil.yml
 --package.json
 -- &amp;lt;entry file&amp;gt;.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These generated files come with basic boiler plate code which you can edit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example for authController (under /controllers)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
 login: function(){},// Add function logic here
 signUp: function(){},// Add function logic here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Example for admin-routes.js (under /routes)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = require('express').Router;

router.post('/delete', (req, res) =&amp;gt; {}); // Add your route logic here
router.post('/another-route', (req, res) =&amp;gt; {}); // Add your route logic here
router.get('/get-here', (req, res) =&amp;gt; {}); // Add your route logic here
router.get('/lol', (req, res) =&amp;gt; {}); // Add your route logic here

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Example for users.js (models) (under /models)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const usersSchema = new Schema({}); //Write your schema here

const users = mongoose.model('users', usersSchema); 

module.exports = users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Example for home.html (under /views)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&amp;gt;
    &amp;lt;title&amp;gt;home&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click &lt;a href="https://github.com/mayankshah1607/node-boiler"&gt;here&lt;/a&gt; to check it out on GitHub. Do give it a star if you find it useful. :)&lt;/p&gt;

</description>
      <category>node</category>
      <category>npm</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
