You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
12 Hrs
40 Min
58 Seg
Curso de Fundamentos de Node.js

Curso de Fundamentos de Node.js

Oscar Barajas Tavares

Oscar Barajas Tavares

HTTP: fundamentos de servidores en Node.js

19/20
Resources

Building web servers is a fundamental skill for any developer working with Node.js. The Node.js native HTTP module provides the tools necessary to build robust web applications without relying on external frameworks. Mastering this module will allow you to better understand how client-server communications work and give you a solid foundation for working with more advanced frameworks in the future.

How to create a basic server with the Node.js HTTP module?

The HTTP module is a fundamental part of Node.js that allows us to create web servers natively. To start working with it, we must first import it into our project:

const http = require('http');

Once imported, we can create a server using the createServer() method. This method accepts a callback function that receives two parameters: request and response, which represent the incoming request and the response that we will send to the client:

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

In this code:

  • res.writeHead(200, { 'Content-Type': 'text/plain' }) sets the HTTP status code (200 means success) and the type of content we will send.
  • res.end('Hello World') sends the response to the client and terminates the connection.

For our server to start listening for requests, we must specify a port and optionally an address:

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

Port 3000 is commonly used in development, although you can choose any port that is not in use in your system.

How to test our HTTP server?

Once the server is running, we can access it through a web browser. If we have configured the server to listen on localhost:3000, we simply browse to that address:

http://localhost:3000

Alternatively, we can use the IP address 127.0.0.1, which is equivalent to localhost:

http://127.0.0.1:3000

How to improve the development flow with the --watch flag?

One of the challenges when developing servers in Node.js is that traditionally we needed to manually restart the server every time we made a change in the code. Tools like Nodemon solved this problem, but now Node.js includes native functionality for this.

Starting with Node.js version 18 (and currently in version 23), the --watch flag was introduced in beta mode, which allows Node.js to detect file changes and automatically restart the application:

node --watch server.js

This command will run our server and restart it automatically whenever it detects changes in the server.js file. This eliminates the need to use external tools like Nodemon during development.

Advantages of the --watch flag

  • More efficient development: No need to manually stop and restart the server.
  • Immediate feedback: Changes are reflected instantly.
  • No external dependencies: No need to install additional packages.

To check the version of Node.js you are using, you can run:

node -v

If you have version 18 or higher, you can take advantage of this feature.

When to move to more robust frameworks?

Although the HTTP module of Node.js is powerful and flexible, for more complex applications it may be more practical to use frameworks that abstract many of the repetitive tasks.

Express.js is one of the most popular frameworks for Node.js, making it easy to create APIs and web applications. It provides an abstraction layer over the native HTTP module, offering:

  • More intuitive route handling
  • Middleware for processing requests
  • Easy integration with template engines
  • Improved error handling

If you are starting to build more complex APIs, considering using frameworks like Express can save you time and effort, while maintaining control over your application.

Mastering the native HTTP module of Node.js is fundamental to understanding how web communications work, even if you later decide to use more advanced frameworks. This knowledge base will allow you to solve complex problems and optimize your applications when necessary. We encourage you to continue exploring and practicing with these concepts to become a more proficient Node.js developer!

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?