🚀A beginner's guide to Express.js
Tags: javascript, beginners, webdev, codenewbie
Published: 2023-08-15 16:36:10

Let us understand what servers are and what Express is in Javascript.

## Table of Contents

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [What is a Server?](#what-is-a-server)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [What is Express in JavaScript?](#what-is-express-in-javascript)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  [Installing Hyper Terminal](#installing-hyper-terminal)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  [Installing NodeJs.](#installing-nodejs)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  [Creating your first Express server](#creating-your-first-express-server)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  [Starting up your server](#starting-up-your-server)

### *What is a Server*?

Servers are the backbone of computer programs, providing top-of-the-line service to clients. These devices operate in the heart of data centers, where their physical presence alone commands respect. It's no wonder they're referred to as servers; they truly serve as the ultimate source of support for the digital age.

### *What is Express in JavaScript*?

Express.js, also known as simply Express, is a robust server-side web application framework that is mainly utilized for building RESTful APIs with Node.js. It is a free and open-source software that is distributed under the MIT License, making it accessible to developers worldwide. The framework is widely popular among developers due to its ease of use and flexibility, making it ideal for building web applications and APIs. In fact, it has been considered the standard server framework for Node.js, with its versatility and efficiency making it a top choice in the development community.

Knowing now that Express is a NodeJs framework. We will be relying a lot on the use of NodeJs, especially for the installation of Express.

### *Installing Hyper Terminal*

Please note that it is completely okay to use your Os' inbuilt command terminal. I prefer using Hyper Terminal, and that is what I'd be using throughout this article.

A guide on how to download and install the hyper terminal can be found on their documentation page [here](https://hyper.is/#installation)


![A view of hyper terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krt5anlihpkdbrmn9qe9.png)

### *Installing NodeJs.*


![NodeJs installation page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fhhsmjgykaezy3scgke.png)

This is a preliminary step to achieve our goal as we will be needing a feature of Nodejs, which is its package manager, NPM (**Node Package Manager**). A complete guide on how to install and set up Node on your computer can be found on the documentation page [here](https://nodejs.org/en/download/). Choose which OS you use, and follow the prompts as you go. I use a windows laptop, and that's why the windows installer is highlighted. 😅😅

### *Creating your first Express server*

Having finished the first two steps, it is time for us to get right into it!!

1. On your hyper terminal, you create a new directory on your desktop with the following command
    
    `mkdir express file`
    
2. Go ahead to create a new file inside this directory by using the following command.
    
    `touch server.js`
    
3. Still, in the same directory, we run `npm init` and follow the prompts where necessary.
    
4. For the purpose of this tutorial, I am using the Atom code editor. To open up our `server.js` file in Atom: we, still in the same directory, use this command `atom .`
    
5. The next step is to install ExpressJs. From the [documentation](https://expressjs.com/en/starter/installing.html), we see that all we have to do is run this command `npm install express` in our terminal (still in the same directory). This installs the Express package onto our local computer.

![Terminal view on installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acbxci719lgytv4zxwp3.jpg)


After this is done, we can go ahead to create our first server!!! 💃🏾💃🏾💃🏾

In our code area, we type in the following...(Don't worry, I'd explain).
    
    ```javascript
    //jshint esversion:6
    
    const express = require('express');
    
    const app = express();
    
    app.listen(3000);
    ```
    

* Our first line of code is a comment (**that is specific to Atom**), telling Atom that we are using ES6 so that it doesn't shoot a bunch of errors at us.
    
* The next line is us *requiring Express*. That is, *fetching Express* from the installation package. We store this in a variable.    
* The next line is fetching the Express module or method, and storing it in a variable `app`.
    
* The last line is us calling a property of the `express` method, `listen`, to listen on port 3000. This simply means that we are creating a (local) server on port 3000.
    

### *Starting up your server*

Back in our terminal, we enter the command `node server.js`. This starts up our server. We will know that this is successful when, after executing the command, our terminal ceases to generate the usual prompts for us. This indicates that the server is on.

We can, however, go a step further by adding an anonymous function to our listen property.

```javascript
//jshint esversion:6

const express = require('express');

const app = express();

app.listen(3000, funtion(){
console.log('The server has started on port 3000')};
```

You will see the console message spin-up on your terminal window when you start the server. This shows you that your server has started running.

*To exit the server on the terminal, type* `ctrl + c` *or* `cmd + c`*.*

There you have it, your very first server!

There are lots and lots of cool and interesting things that we will be doing with Express going forward, but that is all for this article. Catch you next time!