5.13. Building a Basic Server with NodeJS: API Deployment and Hosting
Page 44 | Listen in audio
In section 5.13 of our e-book course, we'll cover one of the most important topics in creating APIs in NodeJS: creating a basic server and deploying and hosting the API. In this chapter, you'll learn how to create a basic server in NodeJS and how to deploy and host your API so it can be accessed from anywhere in the world.
Creating a Basic Server with NodeJS
To create a basic NodeJS server, we first need to install NodeJS and npm, which is Node's package manager. Once installed, we can start creating our server. The first step is to create a new JavaScript file. In this example, let's call it 'server.js'.
In our 'server.js' file, let's start by importing the NodeJS HTTP module, which allows us to create HTTP servers. To do this, we add the following line of code at the top of our file:
const http = require('http');
Now, we can create our server. For this, we use the 'createServer' function from the HTTP module. This function receives a callback that is called whenever a request is made to our server. The 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. Here is an example of how we can create our server:
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, passing the port where we want our server to listen. For example, to make our server listen on port 3000, we add the following line of code:
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
API Deployment and Hosting
Once we have our basic server set up, the next step is to deploy and host our API. There are several options to host our API, such as AWS, Google Cloud, Heroku, among others. In this example, we are going to use Heroku, which is a cloud hosting platform that supports NodeJS.
To deploy our API on Heroku, we first need to create a Heroku account and install the Heroku CLI. Once we have the Heroku CLI installed, we can use the 'heroku create' command to create a new app on Heroku. This command creates a new remote Git repository that we can use to deploy our API.
Before we can deploy our API, we need to make some changes to our 'server.js' file. First, we need to change the port our server listens on to use the port provided by Heroku. For this, we change the line of code where we call the 'listen' function to the following:
server.listen(process.env.PORT || 3000, () => {
console.log(`Server running at http://localhost:${process.env.PORT || 3000}/`);
});
Next, we need to add a file called 'Procfile' to our project. This file tells Heroku how to start our application. For our API, the content of the 'Procfile' should be as follows:
web: node server.js
Now, we can deploy our API. For this, we use the commands 'git add', 'git commit' and 'git push' to add our changes to the Git repository and push them to Heroku. Heroku then installs all of our project's dependencies and starts our application.
Once deployed, our API can be accessed at the URL provided by Heroku. To discover this URL, we can use the 'heroku open' command.
With this, we conclude section 5.13 of our e-book course. Now you know how to create a basic NodeJS server and how to deploy and host your API. In the next chapter, we'll explore more advanced features of NodeJS and how we can use them to create more powerful and flexible APIs.
Now answer the exercise about the content:
What is the process for creating a basic NodeJS server and deploying and hosting an API as covered in section 5.13 of the eBook course?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: