10.2. Creating a Basic REST API with NodeJS and ExpressJS: ExpressJS Installation

Página 67

Creating a basic REST API with NodeJS and ExpressJS is a simple and straightforward process that can be done by anyone with basic programming knowledge. In this chapter, we'll cover how to install ExpressJS and how to use it to create a basic REST API.

ExpressJS Installation

Before you start building your API, you'll need to install ExpressJS. ExpressJS is a framework for NodeJS that simplifies the development of web applications and APIs. It provides an easy way to define routes and handle HTTP requests.

To install ExpressJS, you will first need to have NodeJS and npm (Node's package manager) installed on your computer. If you don't have them installed yet, you can download them from the official NodeJS website.

Once you have NodeJS and npm installed, you can install ExpressJS using the npm command. Open a terminal or command prompt and type the following command:

npm install express

This command will download and install ExpressJS into your project. You are now ready to start building your REST API.

Creating a basic REST API with NodeJS and ExpressJS

Now that you have ExpressJS installed, you can start creating your API. A REST API is an interface that allows interaction between different parts of an application through HTTP requests. Requests can be to create, read, update, or delete data (known as CRUD operations).

Let's start by creating a new file called "app.js". This will be the entry point for our application. At the top of the file, import ExpressJS using the command require:

const express = require('express');

Next, create a new ExpressJS instance:

const app = express();

Now you can start defining the routes for your API. A route is a path in your application that corresponds to a certain action. For example, you might have one route to create a new user, another to get information about a user, and so on.

Let's start by defining a simple route that returns a welcome message. To do this, use the get method of the app object. The first argument is the path of the route, and the second is a function that will be called when the route is accessed:

app.get('/', (req, res) => {
  res.send('Welcome to our basic REST API!');
});

Finally, you need to tell ExpressJS to start listening for HTTP requests. You do this using the listen method of the app object:

app.listen(3000, () => {
  console.log('API is running on port 3000');
});

Now, if you run your application (using the "node app.js" command in the terminal), you will see the message "API is running on port 3000". If you go to "http://localhost:3000" in your browser, you will see the welcome message you set.

Congratulations, you've just created your first basic REST API with NodeJS and ExpressJS! In the next chapter, we'll expand this API to include CRUD operations and connecting to a database.

Now answer the exercise about the content:

What is the process for creating a basic REST API with NodeJS and ExpressJS?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

6810.3. Building a Basic REST API with NodeJS and ExpressJS: Initial Project Setup

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text