Step By Step Building Your First Node.JS Project
Node.js is becoming one of the most important tools to have in today's software world. Agile development is the main development process used by software companies nowadays. You won’t work in any software company without working with an agile and scrum development process, and that is because it allows fast adaption to change. The architecture used with this style of development is usually a micro-service architecture, where you build each module completely independent and decoupled from one another so that they are easily maintained and updated without affecting each other. Now in order for these modules to communicate with each other, they use web services and most of the time we use REST services, and here where node comes handy. In my opinion, Node.js is the best
Node.js s a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is a server-side scripting framework as opposed to Angular.js which is a client-side scripting framework, this means that Node.js is used for backend development and runs on the server, unlike angular.js which used for frontend development and runs on the client (browser).
Now that we have an overview of Node.js let’s start building our first project. To start with Node you need to install npm. npm is the world’s largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well. To install npm run the following command
sudo apt install npm
Then you need to install node, you can do this by going to node official website https://nodejs.org/en/
Now test that you installed node and npm properly by running the following commands
npm -vnode -v
Now we will use npm to create our first node project, First, let’s create our project directory, then we run the init command
mkdir myapp
cd myapp
npm init
Follow the command line prompt and you can leave everything as the default, then it will ask you in the end if everything looks ok.. click yes. Your directory should look something like this
|_myapp
|__package.json
The package.json file consists of all the project settings and other npm package dependencies. If you open the package.json it should look like this
{"name": "myapp","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC"}
The “name” element is the name of your package and if you upload your package to the npm registry it will be under the string that you set as the value of this element, in our case it’s “myapp”.
The “main” element specifies the file that acts as your entry point when you run your project. As you can see, we don’t have the index.js file in our project directory. So now we create that file
touch index.js
sudo chmod 777 index.js
In order to create our first REST resource, we will use the express package. In Node.js, express is your most trustworthy friend. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Our index.js file should look like this.
const express = require("express")var app = express()app.get("/",function(request,response){response.send("Hello World!")})app.listen(10000, function () {console.log("Started application on port %d", 10000)});
If you run the project now you will get “Error: Cannot find module ‘express’ ”, that is because we need first to install the express package to our project and to do that we will use the following npm command
npm install --save express
After running this command, your package.json will now contain the “dependencies” element that contains all the packages that your project depends on. The package.json looks like this now.
{"name": "myapp","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC","dependencies": {"express": "^4.17.1"}}
Now we can safely run our project. It is as simple as running the following command
node index.js
you can also run your program by using the command “npm start”, you can customize the behavior of the “npm start” by adding it to the scripts element in your package.json file as follows
{"name": "myapp","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"},"author": "","license": "ISC","dependencies": {"express": "^4.17.1"}}
When you call “http://localhost:10000/” in your browser, you will see the “Hello world!” message. Notice that the application is still running, because Node.js runs as a server you don’t need TomCat or any other server to run your application and that is one the aspects that make node.js a good choice for fast development.
Congratulations!! you have built your first Node.js program. Next, we will discuss how to build a more complicated and well-structured project.