Getting started with Express.js - Keeping it simple!

March 23, 2020 / 2 min read
javascriptexpressnode
Express JS Icon

This tutorial is prepared to make you familiar with Express.js. Express is a web framework for node.js that allows you to build a minimal and flexible web application. We will go through some basic concepts of Express in this tutorial and demonstrate various topics of express.js such as routing, middleware, route chaining and more.

Prerequisite

I assume you have the knowledge of following topics to follow along with this tutorial.

  • ES6 Syntax and features, as we will be using modern ES6 syntax
  • Knowledge of Node.js and NPM (Node Package Manager)
  • Postman: We will be using it to test the API endpoints

Postman is a popular API end point testing application

What you will learn

Upon completion of this tutorial, you will learn:

  • What is Express and its use case in developing web application
  • How to setup the Express application and create a basic route.
  • Using routing parameters in express
  • How to chain a routing (*Intermediate topic)
  • Understanding and using middleware in Express.

Source Code

What is Express.js ?

"Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications" - Express

As a full-stack javascript developer, I use Express to make a web server and API in the node.js runtime environment. You can simply install the express as a node package in your application. The complexity of creating a server and handling different routes by using the core http module is reduced by the express package.

For example the code for creating http server using http core module looks like:

var http = require('http');

//create a server object:
http
  .createServer(function (req, res) {
    res.write('Hello World!'); //write a response to the client
    res.end(); //end the response
  })
  .listen(8080); //the server object listens on port 8080

In the above code, we have imported the http core module from node and created the server that listens on port 8080. By using express we can acheive same thing simply as:

//this is es6 syntax for importing packages
import express from 'express';

const app = express();

app.listen(8080, () => {
  // on the server
  console.log(`Your Server is running on port 8080`);
});

There are lot of cool features offered by express.js through different methods. You will get indepth understanding of Express while you follow along this tutorial. So lets get started!

Installing Express, Initial Setup

As Express is node package, first we need to install node and npm in our machine. NPM comes along with the installation of Nodejs. You can download and install NodeJS from official site.

After you installed NodeJS and NPM in your machine, simply run the following commands to verify if its installed properly. You will get a version number in return

node -v
npm -v
  1. Making project directory:
mkdir express-app && cd express-app
  1. Creating package.json file:
  npm init
  1. Installing Express:
npm install express
  1. Installing additional tools:
  npm install nodemon

nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected..

  1. Install project dev dependencies:

Since we are going to use ES6 syntax, we need a babel to transform our ES6 code into a code that browser can interpret without error. To install babel and successfully use it on our project we need to install couple of babel packages.

  npm install --save-dev babel-cli babel-preset-env babel-preset-stage-0
  1. Configuring nodemon and babel:

Now its time to configure nodemon and babel in our project. Instead of running npm index.js, we will configure nodemon in our package.json file. Lets change the "start" scripts in package.json file to this:

  "scripts": {
    "start": "nodemon ./index.js --exec babel-node -e js"
  }

Part 2 to be continued.

author-profile-image

Hi, I am Ajeet, a pragmatic software engineer based in Melbourne, Australia. I write an article about modern software development and my side projects. If this site has helped you somehow to learn, I would be grateful if you consider supporting me.

Buy me a Coffee

Comments

Newsletter

Signup my newsletter to get update when any new stuffs comes out!

A books image

Ajeet Chaulagain © 2020. All rights reserved.

Proudly built with React, Gatsby, GraphQL, Typescript, Styled Components, Netlify and Nodejs