Article image Creating a basic server with NodeJS: Installing NodeJS

5.1. Creating a basic server with NodeJS: Installing NodeJS

Page 32 | Listen in audio

5.1. Creating a basic server with NodeJS: Installing NodeJS

NodeJS is an application development platform that allows the execution of JavaScript on the server side. It is built on top of Google Chrome's V8 JavaScript engine, offering a fast and efficient runtime environment. One of the key features of NodeJS is its ability to handle asynchronous I/O operations, making it ideal for building RESTful APIs and microservices.

NodeJS Installation

Installing NodeJS is a simple and straightforward process. It is available for a variety of operating systems including Windows, MacOS and Linux. To install NodeJS, you can download it directly from the official website at https://nodejs.org/. There you will find options to download the LTS (Long Term Support) version, which is the most stable and recommended for most users, or the current version, which contains the latest features and improvements, but may not be as stable as the LTS version.

Once you have downloaded the appropriate installer for your operating system, run it and follow the on-screen instructions. The default installation will install NodeJS, npm (node ​​package manager), which is the package manager for NodeJS, and will also install Node.js command prompt, which is a command line interface for NodeJS.

Creating a basic server with NodeJS

With NodeJS installed, we can now start creating our basic server. For this, we first need to create a new JavaScript file. Let's call it 'server.js'. You can create this file in any text editor or IDE you prefer.

In 'server.js', let's start by importing the NodeJS HTTP module, which allows us to create an HTTP server. For this, we add the following line of code at the top of our file:

const http = require('http');

Next, let's use the 'createServer' function from the HTTP module to create our server. This function accepts a callback that is called whenever a request is made to our server. This callback takes two arguments: a request object, which contains information about the request made, and a response object, which we use to send a response to the client. Add the following code to your 'server.js' file:


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

Finally, we need to get our server to start listening for requests. For this, we use the 'listen' function of our server object, passing the port on which we want our server to listen. Add the following code to your 'server.js' file:


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

Now, if you run your 'server.js' file using NodeJS, you should see the message 'Server running at http://127.0.0.1:3000/' displayed in the console, and if you open a browser and navigate to 'http://127.0.0.1:3000/', you should see 'Hello World' message displayed.

Congratulations! You've just created your first basic server with NodeJS. Although this server is very simple, it serves as a solid foundation for building more complex and powerful APIs.

Now answer the exercise about the content:

What is the key feature of NodeJS that makes it ideal for building RESTful APIs and microservices?

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

You missed! Try again.

Article image Creating a basic server with NodeJS: Setting up the development environment

Next page of the Free Ebook:

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

4 minutes

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