Ajay Sharvesh M P

Continuous Integration & Continuous Deployment Tutor. NodeJs + Jenkins+ GitLab, Series #1.

Welcome Folks!

What is NodeJs?

  1. NodeJs is an Open Source Server Environment
  2. NodeJs uses Javascript on the server
  3. NodeJs is independent to the many operating systems (Windows, Mac, Linux, etc.,.).

What can we do with NodeJs?

  1. It can add, delete and modify the data in the database.
  2. NodeJs runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient for your application.

Let’s jump into practical session of creating a NodeJs App..!

Create Your First NodeJs App

Step #1: Download & Install NodeJs.

To check whether the NodeJs install successfully or not and to check the version, follow the command in terminal/command prompt.

node -v

It prints the version number like this,

v10.16.3

Step #2: Download and Install VS Code

Step #3: Open VS Code and Start creating the app.

code .

VS Code will open, and create the mentioned files,

Create index.js and package.json file. And copy-paste the following code.

In package.json, use this code,

{
“name”: “sample-nodejs-app”,
“description”: “This sample app is to test deploy in jenkins”,
“version”: “0.0.1”,
“private”: true,
“dependencies”: {
“express”: “4.17.1”
},
“devDependencies”: {
“mocha”: “7.0.1”,
“supertest”: “4.0.2”
}
}

The package.json file is used to maintain the dependency version in your application.

Express is the Node Framework, used to listen to the server.

Mocha is the testing framework for Node.

Supertest is for high-level abstraction for HTTP Testing.

Now, we defined the dependencies in the package.json file. Now we have to install them, to install follow this command,

npm install

NPM is Node Package Manager, which is used to puts modules in place so that node can find them, and manages dependency conflicts intelligently. It is extremely configurable to support a wide variety of use cases. Most commonly, it is used to publish, discover, install, and develop node programs.

After executing the above command, you can see the folder in your app called node_modules. That maintains all your dependencies.

Now let’s start writing code in index.js file, copy-paste the following code.

//This imports the node framework -> express
var express = require(‘express’);

var app = express();
/* This responds with "hello world" message for requests that hit our "localhost:3000/" -> this url is root */
app.get(‘/’, function (request, response) {
response.send(‘Hello world!’);
});
//Server listen to port 3000 by default
app.listen(process.env.PORT || 3000);

module.exports = app;

After adding these lines of code in index.js, run this command in the terminal/command prompt.

node index.js

Now copy this URL and paste it into any of the browsers to see the response from the server.

localhost:3000

If you see ‘hello world’ message in the browser, then you’re ready to go.

Message from Node Server

Voila..!

Author • Freelancer • Full Stack • Designer • Android • GSoC