Skip to content

Instantly share code, notes, and snippets.

@RamEduard
Created March 28, 2024 12:46
Show Gist options
  • Save RamEduard/ee140eb7905a477a1a742e7af133bb4a to your computer and use it in GitHub Desktop.
Save RamEduard/ee140eb7905a477a1a742e7af133bb4a to your computer and use it in GitHub Desktop.
Notas - Primer Servidor Web de Node.js

Primer Servidor Web de Node.js

Introducción

Usos comunes de Node

  1. Real-time chats
  2. Internet of Things
  3. Complex SPAs (Single-Page Applications)
  4. Real-time collaboration tools
  5. Streaming applications
  6. Microservices architecture

Código - Primer Servidor de Node

import { createServer } from 'node:http';

// Definición de constantes `const`
const portNumber = 3000;

// Términos: Callback y Arrow Functions
const createServerCallback = (req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World!\n');
};
const onListenCallback = () => {
  // Console log
  // Template literals
  console.log(`Listening on 127.0.0.1:${portNumber}`);
};

const server = createServer(createServerCallback);

// starts a simple http server locally on port 3000
server.listen(portNumber, '127.0.0.1', onListenCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment