In section 5.7 of our course, we'll cover a crucial topic for any developer wanting to build APIs in NodeJS: How to create a basic server, handle data, and return responses. We will present a step-by-step approach so that you can fully understand the process.

Creating a basic server with NodeJS

To start, we need to create a basic server. NodeJS has a built-in module called 'http' that allows us to do this. First, create a new JavaScript file and add the following code:


const http = require('http');

const server = http.createServer((req, res) => {
  // Handling data and returning responses here
});

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

In this code, we are importing the 'http' module and using its 'createServer' method to create a new server. The 'createServer' method takes a callback function that is called whenever a request is made to the server. This callback function takes two arguments: a request object (req) and a response object (res).

Manipulating data

Now that we have our basic server, we need to learn how to manipulate data. Incoming data usually comes in the form of HTTP requests. These requests can contain various types of data, including URL parameters, form data, and JSON.

To access data from a request, we can use the 'req' object. For example, we can get the URL parameters as follows:


const url = require('url');

const server = http.createServer((req, res) => {
  const query = url.parse(req.url, true).query;
  console.log(query);
});

In this example, we are importing the 'url' module and using its 'parse' method to parse the request URL. The second argument 'true' indicates that we want the URL parameters to be parsed as an object.

Returning responses

After manipulating the data, we need to return a response to the client. To do this, we can use the 'res' object. This object has several methods that allow us to send a response back to the client.

For example, we can send a plain text response like this:


const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello world!');
});

In this example, we are using the 'writeHead' method to set the HTTP status code and response headers. Next, we are using the 'end' method to send the response and close the connection.

Conclusion

Creating a basic server, manipulating data and returning responses are fundamental skills for any developer who wants to create APIs in NodeJS. While this is just a basic example, it provides a solid foundation to start building more complex APIs.

In the next section of our course, we'll explore how to create routes, how to handle different types of HTTP requests, and how to work with databases. Stay tuned and keep learning!

Now answer the exercise about the content:

What is the purpose of the 'createServer' method in the 'http' module of NodeJS?

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

You missed! Try again.

Article image Creating a basic server with NodeJS: Using libraries/frameworks to facilitate API development

Next page of the Free Ebook:

39Creating a basic server with NodeJS: Using libraries/frameworks to facilitate API development

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou 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