Saturday, March 28, 2020

A Simple Node.js App

A Simple Node.js App


We can create a basic modular Node.js application by dividing our source code into multiple JavaScript files with aiming high readability and maintainability of the code.

For this, we need to go back to our IDE (Like Atom, Sublime, or any other IDE of your choice) and create the following directory structure. Let all of these files be blank

We can go back to IDE and create a project as,


|---------------app
|                      |----> index.js
|                      |----> sort.js
|--------->index.js
|--------->package.json



Every Node.js project starts with creating a package.json file. This can be considered as a JSON representation of the application and all the dependencies. Package.json can obtain the application's name, the author's name, and all the dependencies that are needed to run the application. We will discuss the dependencies in detail later.

Step 1


Create package.json file

The package.json document can be generated interactively by using the following command in the terminal,

  npm init   

Hit enter and you will be asked to input several attributes, like the name of your application, version, description, and so on. Just enter the values you know, otherwise just keep hitting Enter, in the end, the package.json file will be automatically generated.

It will look something like this,

          {
             "name": "Node example",
             "version": "1.0.0",
             "description": "sort an array",
             "main": "index.js",
             "scripts": {
               "test": "arraytest"
             },
             "author": "**********",
             "license": "ISC"
           }

Step 2


Create index.js

Now move back to the first file we created called index.js. Try to keep this file as small as possible. We are just putting a single line that is requiring the application itself (the index.js file from the /app form subdirectory). Copy and paste the following code into the index.js file and save the file:

 // index.js
        require('./app/index')

Step 3


Create sort.js file

Now we can work in /app subdirectory, First edit sort.js, copy and paste the following script into this file

      // app/sort.js
        function sortedArray (arr) {
          return arr.sort(function(a, b) {
            return a - b;
          });
        }

        module.exports.sortedArray = sortedArray

Step 4


Create \app\index.js file

Open the index.js file from the /app folder to write very simple code, to sort all the items in an array of numbers in increasing order. The index.js file will have an array of numbers to be sort, and the code that does the sorting operation needs to be kept in a different module.

Copy and paste this code into the index.js file in your /app subdirectory.


   const myarr = require('./sort')
        const numbersToSort = [
          31,
          4,
          11,
          2,
          7
        ]
        const result = myarr.sortedArray(numbersToSort)
        console.log(`The sorted array is: ${result}`)

Step 5


Execute the application

To run the application save all these files, go to the terminal and run,

npm start or  node index.js

command. If everything is right, you will get back the sorted array of numbers as a result:


Results will appear like


node terminal


If something went wrong, review the console log carefully and find the issue based on that.