Let’s apply what we’ve learned so far about Node.js and Express.
We can apply what we’ve learned so far by generating a basic Express application that can be used as a boilerplate to build an even more complex application. Let’s navigate to the backend directory, open a terminal, and start!
Code Hello World
using Express
Let’s begin by choosing a port in an Express application that will receive or listen to requests on the server. (We’ll talk about ports in more detail later in this lesson.) The port that’s chosen is the server that listens to the GET
requests on port 3000
and returns a Hello World!
message.
Note: Use the
npm run start
command in the terminal to run the application.
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!') }); app.listen(port, () => { console.log(`App is listening on port ${port}!`) });
The first step is to import the const express = require(‘express’) package into our application. Then, use the require keyword to get access to the Express library. This is the only method for importing Express in Node.js because it uses CommonJS as the module formatting system. As we discussed before, each file in Node.js is a separate module, and we import the entry point of Express. Because Node.js uses CommonJS as the module system, the ECMAScript 2015 (ES2015) syntax won’t work.
import express from "express"
If we try to run the code, we’ll get a syntax error. Although there are work-arounds that can help us use the ES2015 syntax, we have to stick to the CommonJS syntax. That’s because these work-arounds just make the environment setup more complex and challenging.
The const app = express()
command initializes a new instance of express
. Inside the server, we can run many Express applications at the same time. But in this course, we’ll create only one application. Through the app
object, we’ll set up the configurations so that they listen to the incoming HTTP
requests.
The const port = 3000
port is where the app
receives the request. We can think of this port as being a lot like the door that people use to welcome visitors into their home. A house may have many doors, but there is usually only one door that visitors enter through. That’s why one of the most important steps is to configure our server so that it listens to the request coming into a specific port on our local machine.
Now, use app.get('/', (req, res) => { res.send('Hello World!');});
to inform the application to map the root route. The root route is the main route when we open any website. In this case, the root route is mapped to a function that returns a Hello World!
message as a response.
Finally, the app.listen(port, () => { console.log(
App is listening on port ${port}!) });
runs the app
by calling the listen function. The listen
function takes port
as a parameter. We can provide a callback that occurs after the application is ready. This callback logs the port where the application is listening.