Express

Introduction

When it comes to build web applications using Node.js, You will find that creating a server can take a lot of time in addition Node.js is complex, and because of that complexity you will find yourself writing a lot of code which means you’re likely to introduce more errors , Over the years Node.js has matured enough due to the support from community.

Using Node.js as a backend for web applications and websites help the developers to start working on their application or product quickly.

So in this topic, we are going to look into Express which is a Node.js framework for web development that comes with features like routing and rendering and support for REST APIs.

Learning Objectives

  • What is express?
  • Why to use express
  • Express Installation
  • How to create a web server using express.
  • Basic Routing
  • Express middleware

Study

What is Express

According to the Express official website:

Express is fast, unopinionated, minimalist web framework for node.js.

What does that really mean, though? Let’s break this statement.

  • Here, we all know what fast means, it means it has a very high performance.
  • It requires minimum setup to start an application or an API and is fast.
  • By unopinionated, they mean it does not enforces its own philosophy that an application or API should be built in a specific way, unlike Rails and Django. Its flexibility can be calculated by the number of npm modules available, which makes it pluggable at the same time. If you have basic knowledge of HTML, CSS, and JavaScript and how Node.js works in general, in no time you will be able to get started with Express.

Why express?

Express enables you to create a web server that is more readable , flexible, and maintainable, as compared to developing a web server using only the Node HTTP library, which can get complicated for even the most basic web servers.

  • With the Node HTTP library, even to write a simple logic it requires quite a bit of code like we have to manually grab the URL and check for the content type, etc. It is quite tedious.

  • Also, Express is used for both server-rendered apps as well as API’s that serves JSON data. In fact, there are some other node frameworks as well like, Koa.js, Hapi.js, and AdonisJs but Express is by far the most popular Node.js framework.

  • Finally, It is great to use with client-side Javascript frameworks like react and angular as it’s all Javascript and so you don’t need to use multiple languages.

http server VS express server

With Node HTTP library, you can create routes for various endpoints, you can also serve static HTML, CSS, and javascript files. So let’s look at how to create a web server in nodejs using the HTTP library.

const http = require('http');
http.createServer((req, res) => {
   res.writeHead(200, { 'Content-Type': 'text/plain' });
   res.end('Hello World!');
}).listen(3000);
console.log('Server is up at localhost:3000...');

Express makes it easier to build a web server.

const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));

Installing

  • To install express let’s follow the instructions in express official documentation installing section

Creating a web server using express

Now as we installed express, let’s look at the basic syntax of a web server.

  • So, create a file called server.js in the project directory myapp and write the following code in it.
// loading express in server.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
   res.send("Hello, SHA Hackers");
});
app.listen(port, () => {
   console.log(`Server is up at ${port}`);
});

The above code is actually the bare-bones to get the server to run in the browser. Now, Visit localhost:3000 in your browser. You will see the message.

Basic Routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Each route can have one or more handler functions, which are executed when the route is matched.

Route definition takes the following structure: app.METHOD(PATH, HANDLER)

using middleware

Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls.

Middleware functions: are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

Resources

Additional resources

In this section you can find a lot of helpful links to other content. This is a supplemental material for you if you want to dive deeper into some concepts.