Free Ebook cover How to create APIs in NodeJS from basic to advanced

How to create APIs in NodeJS from basic to advanced

5

(1)

149 pages

Creating a basic server with NodeJS: Manipulating data and returning responses

Capítulo 38

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app


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.

The createServer method in the http module of NodeJS is used to create a new server. It takes a callback function as an argument, which is called whenever a request is made to the server. This enables developers to define how to handle requests and responses on the server.

Next chapter

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

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.