Getting to Know Node.js (Part V)
Tags: node
Published: 2020-12-10 17:22:32

---
title: Getting to Know Node.js (Part V)
published: true
series: Getting to Know Node.js
description: Part V: Express.js
tags: node
cover_image: https://images.unsplash.com/photo-1520169178510-cc33b099a97e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1525&q=80
---

Welcome to part 5 of my Node.js series! Now that Thanksgiving is out of the way I can get back to documenting my learning path (recursively). Today I will jump right into talking about interacting with the back-end using Express.js.

##Express.js##

Express.js is a JS framework that allows us to write our back-end logic in a simplified manner. This means that the number of features is rather limited, unfortunately.
(reword)-> But what it can do with its features saves time in terms of defining routes, defining request methods, setting event listeners, all of which is handled under the hood with Express. <-(reword)

Here is a basic example of how Express interacts with the server.


```
const express = require(“express”);

const app = express();

const port = process.env.PORT || 3000;

app.use(“/“, (req, res) => {
  console.log('Hello world')
  }
   
app.listen(port => `Server is listening at port ${port}`);

```
In the example above we import the Express module, then assign it to an `app` object. The `express` function creates our Express application for us. For legibility I also assigned the port our server will be listening from to a variable `port`, using `process.env.PORT`, or 3000. 

The `app` object we used also has built-in methods that we can use for routing, rendering, and other application behaviors. In our code we used `app.use()` to mount a path `/` to the Express object. This object receives a specified path as a string, and the necessary middleware functions to run when the requested path matches the first argument. In this case, all that happens is print a message to the console.

##Middleware##
Express contains a few built-in middleware functions as well as many third-party middleware for handling the sending and receiving of data. Middleware functions receive an HTTP request body `req`, a response body `res`, and a method `next()`, which calls the next middleware function, but more on that later. Apart from that, we use middleware functions to write changes to a request-response cycle, finish interaction with a server, or even call another middleware function in the stack.

```
app.use(“/“, (req, res) => {
  console.log('Hello world')
  }

```
Here, we have an application-level middleware, which is an instance of the app object, along with a handler method `get()`. The handler is used to indicate the HTTP request-method and receive both the specified path to mount at the application level, and the middleware function. Normally, if a middleware function does not end the server connection, then it must call the next middleware function to handle the connection. This pattern continues until the final middleware in the stack is called.
Lastly, in our example we call `app.listen`, which will act as our listener on a specified port. In this case, our `port` variable.

Thanks for reading! Catch my next post later today. Happy Holidays!