Ready to boost your skills and learn one of the hottest web development platforms today? Let’s start the countdown.
15 Minutes to Go: Download Node.js
The first step to developing your first API is to download Node.js. To do that, head to Nodejs.org and click the download button. Grab the LTS version (LTS stands for long-term support), as it will be the most stable release of Node.js.
Once downloaded, run the installer and follow the installation instructions.
If you are running Linux or one of the Windows Subsystem for Linux distros, you can download Node.js from your distro’s package manager rather than downloading the installer from the website. For example, on Ubuntu systems, you can run:
sudo apt install nodejs
Whether you download Node.js from your package manager or the website, in 2-3 minutes, you’ll have the latest version of Node.js on your computer.
12 Minutes to Go: Create Your Project’s Directory
With Node.js downloaded, the next step is to open your favorite terminal program and create your project’s directory.
If you’re running macOS or Linux, type the following commands:
mkdir ~/my-new-node-api cd ~/my-new-node-api
On Windows, type these two commands with the Windows back slash replacing the Unix-style front slash:
mkdir ~new-node-api cd ~new-node-api
Now that you have initialized your project’s directory, you’re ready to move on to the next step.
11 Minutes to Go: Initialize Your Node.js Project
With your new project’s directory created and your terminal session currently in that directory, the next step is to initialize your project.
To do that, run this command:
Once this command runs, you’ll have to set a few parameters for the package.json file. This file defines your new Node project.
You can keep pressing enter, accepting all the default prompts. Or, you can define the prompts yourself. When asked about the entry point, please keep it as index.js. We will modify this file in later steps.
9 Minutes to Go: Install Express
We’re going to use Express to create and serve the API. To install Express to your package, enter the following npm command:
npm install express
The Node Package Manager (npm) will install the Express framework in your newly created project.
6 Minutes to Go: Create the API
You can create your API in your new Node project with Express!
Create a new file called index.js in your project’s main directory and enter the following code into it:
const express = require('express');
const app = express();
app.get('/', (req, res) =>
res.send('Hello! This is my brand new Node API!');
const port = 3000;
app.listen(port, () => console.log(`Serving my new API on port $port...`));
Save your index.js file.
Please take a little time to look at the above code and think about what it does. The code above imports the Express framework and creates a new HTTP GET API, which sends down some text when a user hits it. It then starts listening for HTTP requests on port 3000.
1 Minute to Go: Try Your New API!
As your project has everything necessary to run your new API, the next step is to try it out! To do that, run the following command:
You’ll see a line on your command prompt that indicates the server has successfully started:
Serving my new API on port 3000…
Windows computers may prompt you to approve some firewall permissions. Accept opening port 3000 so you can see your app work.
In your favorite web browser, go to http://localhost:3000. You will see your server return the “Hello!” text that you set in your code.
Explore the Possibilities that Node.js Offers
Congratulations! You have set up your first Node.js project in less than 15 minutes.
Node is powerful and popular because it makes setting up APIs and other dynamic web experiences very straightforward. To explore Node further, try changing the text that your API returns. Or try changing the port from 3000 to 5000.
Indeed, play around with the code in your index.js file to see how easy it is to modify your app and create new functionality. You’ll quickly discover just how powerful Node can be.