5.2. Creating a basic server with NodeJS: Setting up the development environment

Página 33

5.2. Creating a basic server with NodeJS: Setting up the development environment

Before we dive into creating a basic server with NodeJS, it's crucial to properly set up the development environment. This section will guide you step-by-step through that process.

Installing NodeJS

The first step is to install NodeJS on your machine. NodeJS is a JavaScript runtime environment that allows us to run JavaScript on the server. To install NodeJS, go to the official NodeJS website (https://nodejs.org) and download the latest version. Installation is quite simple and straightforward, just follow the on-screen instructions.

Verifying NodeJS installation

After installation, you can verify that NodeJS installed correctly by opening a terminal or command prompt and typing the following command: node -v. This command should return the version of NodeJS you have installed.

Installing NPM

The next step is to install NPM (Node Package Manager). NPM is a package manager for NodeJS and is used to install third-party libraries and tools. Fortunately, NPM is automatically installed when you install NodeJS, so you don't have to worry about that.

Verifying NPM installation

To verify that NPM installed correctly, you can open a terminal or command prompt and enter the following command: npm -v. This command should return the version of NPM you have installed.

Setting up the NodeJS project

Now that we have NodeJS and NPM installed, we can start configuring our NodeJS project. The first step is to create a new directory for our project. You can do this using the mkdir command in your terminal or command prompt. For example: mkdir my-nodejs-project.

Next, navigate to the project directory using the cd command. For example: cd my-nodejs-project.

Once inside the project directory, you must initialize a new NodeJS project using the npm init command. This command will create a new file called package.json in your project directory. The package.json file is used to manage your project's dependencies and scripts.

Creating a basic server with NodeJS

Now that we have our development environment set up, we can start creating our basic server with NodeJS. To do this, we first need to create a new JavaScript file in our project directory. Let's call this file server.js. You can create this file using the touch command in your terminal or command prompt. For example: touch server.js.

Next, open the server.js file in your favorite text editor. Inside this file, we will import the http module from NodeJS, which allows us to create an HTTP server. Here is the code to do this:


const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

This code creates a basic HTTP server that listens on port 3000 and responds with 'Hello World' to all requests. You can start the server using the command node server.js in your terminal or command prompt.

Congratulations, you've just set up your development environment and created a basic server with NodeJS! In the next chapter, we'll explore how we can expand on this basic server to create complete RESTful APIs.

Now answer the exercise about the content:

What is the procedure to verify that NodeJS and NPM are installed correctly?

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

You missed! Try again.

Next page of the Free Ebook:

345.3. Creating a basic server with NodeJS: Understanding the API concept

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