DEV Community

Mayank Shah
Mayank Shah

Posted on

Automatically generating boiler plate code for NodeJS REST APIs

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.

This is when I developed an npm module for personal use - "node-boiler", which I later made open source (You can find it here). 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! :)

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.

Let's give this a try.

I must first install this library globally:

$ npm i -g node-boiler
Enter fullscreen mode Exit fullscreen mode

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

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'

Enter fullscreen mode Exit fullscreen mode

It defines your project structure in the most obvious way.

Once this is done, run the following command :

$ nodeboil
Enter fullscreen mode Exit fullscreen mode

You're likely to see the following output:

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*******
Enter fullscreen mode Exit fullscreen mode

And your project directory will now look like

 --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
 -- <entry file>.js
Enter fullscreen mode Exit fullscreen mode

These generated files come with basic boiler plate code which you can edit.

  • Example for authController (under /controllers)
module.exports = {
 login: function(){},// Add function logic here
 signUp: function(){},// Add function logic here
}
Enter fullscreen mode Exit fullscreen mode
  • Example for admin-routes.js (under /routes)
const router = require('express').Router;

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

module.exports = router;
Enter fullscreen mode Exit fullscreen mode
  • Example for users.js (models) (under /models)
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;
Enter fullscreen mode Exit fullscreen mode
  • Example for home.html (under /views)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home</title>
</head>
<body>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Click here to check it out on GitHub. Do give it a star if you find it useful. :)

Top comments (0)