Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Setting Up Your Express.js Project

Recommended resources

Introduction

Welcome to your backend engineering crash course! Our single goal over the next 12 hours is to get you fully prepared for your certification exam. We'll move quickly, focusing entirely on the practical skills you need to build, test, and deploy a production-ready API.

In this first lesson, we'll lay the foundation for our entire project. You will learn how to initialize a brand new Node.js project and install Express, the web framework we will use to build our API. Let's get started.


Step 1: Initializing Your Node.js Project

Every Node.js project begins with a package.json file. This file acts as the project's manifest, tracking metadata, dependencies (the external packages your project uses), and custom scripts.

The fastest way to create this file is with the Node Package Manager (npm), which comes bundled with Node.js.

  1. Open your terminal or command prompt.
  2. Create a new directory for your project and navigate into it:
    mkdir backend-api
    cd backend-api
    
  3. Now, run the initialization command:
    npm init -y
    

The -y flag tells npm to use the default settings for everything, creating the package.json file instantly. You can open this file in your code editor to see what it contains. It will look something like this:

{
  "name": "backend-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Step 2: Installing Express

With the project initialized, our next step is to install Express. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It will be the core of our API.

To install it, run the following command in your terminal:

npm install express

This command does two things:

  1. It downloads the express package and all its dependencies into a new folder called node_modules.
  2. It automatically adds express to the dependencies section of your package.json file. This is crucial for ensuring that anyone else (or any deployment service) can replicate your project's setup just by running npm install.

The following short video provides a fast-paced walkthrough of the exact two steps we just covered: project initialization and installing Express.

RESTful APIs in 100 Seconds // Build an API from Scratch with Node.js Express

Watch this clip from the 'RESTful APIs in 100 Seconds' video by Fireship to see the npm init -y and npm install express commands in action.

Watch from 03:08 to 03:30. Notice how running the commands creates the package.json file and then adds Express to its dependencies.


Step 3: Creating a Minimal Server

Installing a package is one thing, but let's make sure it works. We'll create a minimal web server that listens for connections. This confirms our setup is correct and gives us a starting point for the next lessons.

  1. In your project's root directory (backend-api), create a new file named index.js. This is the default entry point listed in your package.json.

  2. Add the following code to your index.js file:

    // 1. Import Express
    const express = require('express');
    
    // 2. Create an Express application
    const app = express();
    
    // 3. Define a port number
    const PORT = 3000;
    
    // 4. Start the server and listen for connections
    app.listen(PORT, () => {
      console.log(`Server is running on http://localhost:${PORT}`);
    });
    

    Code Breakdown:

    • require('express'): This line imports the Express module.
    • express(): This function call creates a new Express application instance.
    • app.listen(): This function starts up a server and has it listen on a specified port for incoming connections. The second argument is a callback function that executes once the server is successfully running.
  3. Save the file and run it from your terminal:

    node index.js
    

You should see the following output, confirming your server is live:
Server is running on http://localhost:3000

You now have a running Node.js server! It doesn't do anything yet, but the foundation is successfully in place. You can stop the server by pressing Ctrl + C in your terminal.


Conclusion

In this lesson, you took the critical first steps in building a backend API. You learned how to use npm init -y to create a package.json file and npm install express to add the core framework to your project. Finally, you wrote a few lines of code to create and run a minimal Express server, verifying your initial setup.

Key Takeaways:

  • package.json is the manifest file for a Node.js project.
  • npm init -y is the fastest way to generate a package.json file.
  • npm install <package-name> adds a dependency to your project's node_modules folder and package.json file.
  • const app = express() creates an Express application.
  • app.listen() starts the server.

In the next lesson, we will build upon this foundation by creating a professional folder structure to organize our code and setting up environment variables with dotenv to manage configuration securely.