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

Página 66

10.1. Building a Basic REST API with NodeJS and ExpressJS: Installing NodeJS

To start creating a basic REST API with NodeJS and ExpressJS, the first step is to install NodeJS. NodeJS is a software development platform that enables server-side JavaScript execution. It is used to create scalable and efficient web applications, making it a popular choice for API developers.

The installation of NodeJS is quite simple and straightforward. First, go to the official NodeJS website at https://nodejs.org/en/. There, you will find two versions available for download: the LTS (Long Term Support) version and the Current version. The LTS version is more stable and recommended for most users, while the Current version contains the latest features.

After downloading, run the installer. The installation wizard will guide you through the process. Make sure the option to install 'npm package manager' is selected as you will need it to install ExpressJS and other packages.

To verify that NodeJS installed correctly, open a terminal or command prompt and type node -v. This should return the NodeJS version you installed.

Installing ExpressJS

ExpressJS is a framework for NodeJS that simplifies the development of web applications. It provides a simple framework for routing, handling requests and responses, and other common functionality in web applications.

To install ExpressJS, you'll use npm, which is NodeJS's package manager. In the terminal or command prompt, navigate to the directory where you want to create your project and type npm init -y. This will create a new package.json file, which is used to manage your project's dependencies.

Next, install ExpressJS by typing npm install express. This will download ExpressJS and add it to your project's dependencies.

Creating a Basic API

Now that you have NodeJS and ExpressJS installed, you're ready to start building your API. First, create a new file called app.js in your project directory. This will be the entry point for your application.

In the app.js file, start by importing ExpressJS with the command const express = require('express'). Then create a new instance of ExpressJS with the command const app = express().

To create a simple route, you can use the app.get() method. For example, to create a route that responds to GET requests at the root of your site, you could use the following code:

app.get('/', (req, res) => {
  res.send('Hello, World!')
})

This defines a function that will be called whenever someone accesses your site's root with a GET request. The function sends the response 'Hello, World!'.

Finally, to start the server, use the app.listen() method. For example, the following code starts the server on port 3000:

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

To start your application, type node app.js in the terminal or command prompt. You should see the message 'Server is running on port 3000', and you can now access your API at http://localhost:3000.

Congratulations! You've created your first basic REST API with NodeJS and ExpressJS. From here you can start adding more routes, manipulate request and response data, connect to a database, and much more.

Now answer the exercise about the content:

What is the first step to create 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:

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

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