5. Creating a basic server with NodeJS
Page 31 | Listen in audio
Creating a Basic Server with NodeJS
NodeJS is a platform that allows the execution of JavaScript codes on the server side, that is, on the back-end of the application. One of the main features of NodeJS is the creation of HTTP servers, which is fundamental for building APIs. In this chapter, we will learn how to create a basic server with NodeJS.
1. Installation of NodeJS
Before we start creating our server, we need to have NodeJS installed on our system. To do so, go to the official NodeJS website (https://nodejs.org) and download the latest version compatible with your operating system. After installation, you can verify that NodeJS installed correctly by opening a terminal and typing the command 'node -v'. This command should return the version of NodeJS that was installed.
2. Creating the Project
With NodeJS installed, we can start creating our project. First, create a new folder for your project and open that folder in the terminal. Then initialize a new NodeJS project with the 'npm init' command. This command will create a new 'package.json' file in your project, which is where the project settings and dependencies will be stored.
3. Creating the Server
Now that we have our project set up, we can start creating our server. First, create a new file called 'server.js' in your project. This file will be the entry point to our server.
In the 'server.js' file, let's start by importing the 'http' NodeJS module. This module allows us to create HTTP servers. To import a module in NodeJS, we use the 'require' function. Then, at the top of our 'server.js' file, add the following line of code:
const http = require('http');
With the 'http' module imported, we can use the 'createServer' function to create our server. This function takes a callback function as an argument, which will be called whenever a request is made to our server. This callback function takes two arguments: a request object and a response object.
const server = http.createServer((req, res) => {
res.end('Hello world!');
});
In the callback function, we are simply sending the string 'Hello world!' as a response to all requests our server receives.
4. Starting the Server
With our server created, we need to start the server so that it starts listening for requests. For this, we use the 'listen' method of our server object. This method receives as an argument the port on which the server should listen. For example, to start the server on port 3000, add the following line of code at the end of the 'server.js' file:
server.listen(3000, () => {
console.log('Server running on port 3000');
});
Now, if you open a terminal and run the command 'node server.js', you should see the message 'Server running on port 3000'. If you open a browser and go to 'http://localhost:3000', you should see the message 'Hello world!'.
5. Conclusion
In this chapter, we learned how to create a basic server with NodeJS. Although this is a very simple server, it is the foundation for building more complex APIs. In the next chapter, we'll learn how to handle different types of requests and how to send more complex responses.
Now answer the exercise about the content:
What is the function of the 'npm init' command in the process of creating a server with NodeJS?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: