NodeJS for non Javascript developer : Part 3

Arun Singh
6 min readSep 14, 2018

--

This article is the continuation of my previous article NodeJS for the non-Javascript developer: Part 2 and Part 1.

Photo by Clément H on Unsplash

We have already covered all the required stuff for building a NodeJS application. Let’s go deep dive to see different features of NodeJS.

Module: Module is a file containing multiple functions inside it which can be called in other javascript files or when we add multiple functions inside a file which can be used in other javascript files we take help of modules.

Let’s create a file moduleDemo.js and create a function as below

var testModule = function(){
console.log(“This is module”);
}

Let’s import this method in another file. To do this add the below line.

module.exports.test = testModule;

Note : In NodeJS module.exports or simply exports is an object which is included in every JavaScript file. Here module refers to current file and exports makes it available to other projects.

Create another file app.js in the same folder and add the lines as shown in below image.

require is the keyword to import the file.

var modDemo = require(“./moduleDemo”);

Here “./” in “./moduleDemo” is used for the current directory.

And the function can be called as below.

modDemo.test();

Node Package Manager: Also know as npm. Its purpose is to download the code which is already developed by someone else in your project. If you are familiar with java it’s more like a maven.

Let’s see how it works.

Create a folder npmdemo and navigate to that folder and run below command.

$ npm init

After running this command, the prompt will ask for few questions about the project.
package name -> Any package Name you can provide
version: (1.0.0) : -> leave it blank
description: -> Provide description about the project
entry point: -> This is the file where the program starts executing
test command: -> leave it blank for now
git repository: -> leave it blank for now
keywords: -> keywords related to your project
author: -> your name
license: -> Any license if required

After that, you can see something like below.

Press yes in the end. And after that, you will see a package.json file in the same directory.

We will see this in more details. Now let’s create a file index.js in the same folder.

And run node . on terminal.

This command will read the package.json file and look for the entry point and run that file. In this case, it is index.js.

Now let’s start adding the module which is developed by others which can be found on https://www.npmjs.com/.

Let’s start with the color module that can help in printing console in the different color. Run the below command on terminal.

save is for saving the dependencies to the current project.

npm will install colors module from here. Open the package.json and verify the dependencies.

To use this module inside your project, use “require” keyword.

var col = require(‘colors’);

Here we are importing colors module and for that, require keyword is used.

And variable col can be used as below.

console.log(col.green(‘hello’));

console.log(col.red.underline(‘i like cake and pies’))

And the output will look something like this.

Now the other way to run the script using npm command. Create a file app.js in the same folder.

Update the script tag of package.json file.

“scripts”: {

“test”: “echo \”Error: no test specified\” && exit 1",

“start” : “node app.js”

},

Using this package.json, npm can run the script which finally executes app.js.

$ npm start

When above command will run on the terminal it will call the start tag of scripts and executes the app.js.

Let’s use some famous npm package which is frequently used.

express.js : It is a web framework for NodeJS.

Create a folder expressdemo, navigate to the folder using the terminal and execute npm init and pass all the default value.

After that run :

Open package.json and verify the dependency.

Open index.js and write below script.

Here,

var express = require(‘express’)

var app = express()

Importing and creating the express object.

GET is the HTTP method.

app.get(‘/demo’, function (req, res) {

res.send(‘Hello World!!!’)

})

In the above code, any GET request with URL appended with /demo will be handled by this function and in response “Hello World!!” Will be sent to browse.

app.listen(3000)

This app is listening on port 3000.

Open the browser and type the URL http://localhost:3000/demo. You will see Hello World!!! as an output.

Yargs: It is used to build interactive command line tools. This is helpful in sending command line parameters.

To install this use below commands

Check the dependencies in package.json file.

Open the index.js file and add the below scripts.

Here yargs is imported into the file and the script is checking if the user passed some arguments with name “name”.

Chai: It is an assertion library which is used for unit testing in NodeJS.

This can be installed using below commands.

Open the index file and import the chai module.

var expect = require(‘chai’).expect;

Above example is a successful test case. Let’s modify the test and see if it passes.

In the above example, id is changed to id1 and this is failure test case.

More about chai can be found here.

This is the end of NodeJS series. Hope you enjoyed :). Feel free to ask any question.

--

--