Learn Node.js by example — Part 1

Eniola Lucas Fakeye
5 min readSep 28, 2017

Javascript is everywhere. Gone are the days when you need to know Javascript and some other server side language before you can build a complete web application, thanks to Node.js.

Node.js is a platform that allows you to write Javascript on the server. With Node.js you can build scalable and fast applications, it is also perfect for data intensive applications.

SAP, LinkedIn, Netflix, GoDaddy and Microsoft are some of the popular companies that use Node.js

The aim of this tutorial is teach you how to setup a simple Node server, how to use node modules and node package manager and a gentle introduction to MongoDB. To achieve this, I’ll be building a very simple back-end for a Todo app, REST API endpoints will be utilized to expose resources to the client-side setup.

Getting Started.

Before you get started, you need to install node on your computer. If you haven’t, visit the Node.js website and follow the instruction to download latest version of Node. To confirm if node has been installed, go to your command line and run node -v you should see the version of node installed on your system.

Node Package Manager.

After the installation of node, setting up our application is next, using the Node Package Manager (npm) commands.

So then, what is Node Package Manager?

Node package Manger is an entity that act as a store for node library or modules (both are code written by someone else so that you won’t re-invent the wheel if you want to solve a similar problem) and also allow the management of the modules e.g installation and updates via the command line interface.

To get started with npm in our shining new project, navigate to the project folder via command line (I name my project folder ‘TodoServer’) and run ``npm init`` in the Command line Interface to create a ‘package.json’ file in the project directory.

You will be asked some questions like the project name, project version and so on, in case you’ll like to go with the default value (which is not bad in this case) add — -yes flag to the npm init command like so npm init --yes.

This is what I get after running ‘npm init’ command

Flags are options that are added to basic commands to make them perform extra task, in this case to give a default value for all the fields. Note, all valid Node Package Manager commands starts with ‘npm’.

Basically, the package.json file contains a meta data of your project (i.e the name of the project, the version, the license type, the name and version of node libraries being used in the project and so on). You can also define custom npm commands in the package.json file.

My package.json file.

Getting started with express.

Express is a very light Node.js framework that enables developers to build web applications and APIs. We’ll use express to build API for theTodo app.

  1. Installation.

Before you can use express, you need to install / download it as a npm package. To install express, navigate to the project directory and run the following command npm install express --save

If the installation is successful, it will reflect in the CLI and also in the package.json file. If you open the package.json file, express must have been added as a dependency together with a folder named ‘node_modules’ that will hold the source code for installed node modules or libraries. Also, a file named ‘package-lock.json’ named will be auto generated. The auto generated file give developers the power to switch between different states of node modules or libraries installed in a node project, it also ensure that developers working on a node project install exactly the same version of node modules or libraries, among other things.

Express has been added to package.json

2. Server setup

To setup express server, create a file named index.js. The name of the file is index.js because that is what we specified as the entry point to our node application in the package.json file.

Once this is done, copy and paste the code below into your index.js file

// import express framework using es6 syntaxvar express = require('express');// create an instance of express called appvar app = express();// specify the port number through which your app will be servedconst PORT = 9000;// use express get method to create aapp.get('/', function(req, res) {res.status(200).send("Hello World!");});// listen to request sent to the portapp.listen(PORT, function() {console.log("Server running on Port", PORT);});

With the block of code above, we’ve set up a simple express server. In the block of code above:

a. The express framework was imported as ‘express’, to make it accessible in the project. This was done using es5 require() function.

b. Creating an instance of express is next. It will be named ‘app’. A constant named ‘PORT’ is also created with the sole purpose of holding the value of the port on which the app will be served.

c. We use the get method of express to create a route which can only be accessed when a GET request is made to the server. Express has a lot of methods, including all the HTTP verbs i.e GET, PUT, DELETE, POST and PATCH . These methods can be utilized to create a very powerful server.

To create a route in express, after deciding the type of request that is allowed to be made to the route, a path will be specified and an ‘handler’ i.e a function that accepts two parameters, req and res . The handler function contains the logic or block of code that will be executed when a user navigate to this route, in this case the classic ‘Hello World!’ message together with HTTP status code of ‘200’ will be sent as HTTP response.

Basically, the req parameter is an object that represent HTTP request made to the server and contains properties like headers, body, request query string e.t.c.

Response from the server is handled by the response object represented by the res parameter. The response object will manage the HTTP response that express will send after receiving a request, This can be data, status code and the likes.

d. ‘listen()’ is an express method that accepts two parameters, the port number and an optional callback function. Listen will allow the express server to listen to request on a particular port.

--

--

Eniola Lucas Fakeye

Software Engineer | Startup Enthusiast | Passionate Learner