Introducci贸n a Node.js
Introducci贸n Node.js
Instalaci贸n y configuraci贸n del entorno de Node.js
Primer proyecto con Node.js
Quiz: Introducci贸n a Node.js
M贸dulos y gesti贸n de paquetes
Tipos de M贸dulos en Node.js
Gesti贸n de Paquetes con NPM
Creaci贸n de un Paquetes con NPM
Publicaci贸n de Paquetes con NPM
Quiz: M贸dulos y gesti贸n de paquetes
M贸dulos nativos en Node.js
Introducci贸n al M贸dulo FS de Node.js
Leer y escribir archivos en Node.js
M贸dulo fs: Implementar transcripci贸n de audio con OpenAI
M贸dulo Console: info, warn, error, table
M贸dulo Console: group, assert, clear, trace
M贸dulo OS: informaci贸n del sistema operativo en Node.js
M贸dulo Crypto: cifrado y seguridad en Node.js
M贸dulo Process: manejo de procesos en Node.js
Timers: setTimeout, setInterval en Node.js
Streams: manejo de datos en tiempo real en Node.js
Buffers: manipulaci贸n de datos binarios en Node.js
Quiz: M贸dulos nativos en Node.js
Servidores con Node.js
HTTP: fundamentos de servidores en Node.js
Servidor nativo y streaming de video en Node.js
You don't have access to this class
Keep learning! Join and start boosting your career
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.
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.
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
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.
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.
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:
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
Want to see more contributions, questions and answers from the community?