In this course, you will learn how to create an initial setup for an Express server.
Learning Objectives
- Install Express
- Implement the top-level Express function
- Define the port variable and explain its purpose
- Learn about internet ports in general
- Implement the Express listen method
- Modify the package.json file to create a start script
Intended Audience
- This course is intended for anyone who wants to learn about Express.
Prerequisites
Introduction to Node.js - What Is a Node Module and Non-Blocking I/O
Introduction to Node.js - CommonJS Modules
Introduction to Express - Express Setup. Express is an open-source popular web framework for Node.js. It is used by enterprises such as the ones listed here and many more. In this video, I'm going to go over the initial setup of an Express application. Transitioning to the lab environment. On the screen, I have the package.json file open in the Cloud Academy lab environment. I will begin by installing Express. In the terminal below, I will type npm install express. Now I will speed up the installation process. Now in the package.json file, Express is listed under dependencies. Now to set up Express, I've already created a server.js file, and this file will hold the code for the Express environment.
And with this file name, I'm implying that I will be building a server, and I am building a server because the Express environment will be listening for incoming HTTP requests and sending a response back based on what is requested. To begin, I need to import Express using a require statement using common js syntax, const express = require('express'); Next, I need to set up the Express function, const app = express(); The Express function is a top-level function that is exported by the Express module. This allows access to different Express methods. The method that I will be using in this video is the listen method. For the listen method, I need a port value. For the Cloud Academy lab environment, that port will be 3001. For security reasons, no other port will be allowed to work besides 3001 unless stated in the lab instructions. So, I will type const PORT = 3001; I'm using all caps because this variable is storing a constant value. So, what is a port?
A port is a virtual point where network connections start and end. These ports are handled by the operating system and allow for communication between devices on the Internet. For example, you're using a port to view this video. There are over 65,000 ports and some of them are reserved for specific purposes, such as port 80 which is used in traditional HTTP requests. Next thing you need to set up Express to listen for incoming requests, app.listen(), and I'm going to pass through two arguments. First, a port variable, and second an arrow function. In this arrow function, I'm going to console.log a template literal, 'Server is listening on port' using a template string expression, dollar sign, curly brackets, the ${PORT} variable, which is in all caps.
With the listen method and the port value, I am telling the server to listen to all requests through this specific port. And this initial setup can be considered boilerplate. I can use the same code repeatedly in other node projects to set up an Express server. Now in order to run this, I simply need to run node on the server.js file, but I'm going to take this opportunity to create a simple start script. Going back to the package.json file, I have this default test script and I'm going to change the script from what's in here currently to node server.js. And I'm going to change test to start. Switching back to the server.js file as a reference in the terminal, I will type npm start, and in the terminal, there's a console.log message that says 'the server is listening on port 3001'. With this, I have the foundational structure of an Express application. And that's it. Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.