Installing Express
Overview
This section will focus on helping you set up Express.js (or informally known as Express). It is a web application framework for Node.js that is free and open-source. Express is used to provide server-side logic for web and mobile applications.
Installation Steps for Express
In the introduction, you will have created a new project folder with all of the subfolders and subfolders required. First, you'll install express and any dependencies, and then walk you through how to create an Express server, so you can serve content to your front-end (browser).
-
Install
nodemon
:npm install --save-dev nodemon
-
Install Express:
npm install express
-
Create a
src
folder in the root directory (if you haven't before):mkdir src
-
Create an
app.js
file in thesrc
directory (if you haven't before):touch src/app.js
-
Open the
app.js
file created above and add the following code:const express = require('express'); // (1) const app = express(); // (2) const port = 3000; // (3) app.get('/', (req, res) => { // (4) res.send('Hello World!'); }); module.exports = app; // (5)
- This line of code will import the express module into your app.js entry point file.
- This line of code will create a new Express application.
- This line of code will store the number of the port that we will be using to connect to your localhost.
- The GET method route will allow our application to send the string "Hello World!" as a response when we connect to our localhost. You will be learning more about HTTP methods like GET in a later section.
- Export the Express application so that
server.js
can use it.
-
Create an
server.js
file in the root directory:touch server.js
-
Open the
server.js
file created above and add the following code:const app = require('./src/app'); // (1) const port = 3000; // (2) app.listen(port, () => { // (3) console.log(`🚀 Server started successfully, listening on port ${port}: http://localhost:${port}`); });
- Import instance of Express Application created in
src/app.js
. - Specify port number for server to listen on.
- Start server and listen on specified port.
- Import instance of Express Application created in
Start Express Server
In this last section, you will learn how to run your Express server on your localhost.
-
Open the
package.json
file in the root directory and add a script to start Express:{ ... "scripts": { "start": "nodemon server.js", // (1) "lint": "eslint . --fix --ext .js" }, ... }
- This script allows you to use
npm run start
ornpm start
to start the Express server.
- This script allows you to use
-
Run the script create above to start the Express server:
npm start
-
Open your internet browser of choice and type in the URL for the Express server:
Success
Once the page loads, you should see "Hello World!" in your browser. This means that you have successfully installed and created your first working server!
Conclusion
By the end of this section, you will have successfully learned the following:
- How to install Express
- How to create a working Express server on your localhost
Great job 🤗. You can go ahead and click on the link below to move on to the next step: