Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/brandonvergara1220-del/Mini-Proyecto-Backend-NodeJS/llms.txt

Use this file to discover all available pages before exploring further.

Local development lets you build and test the full registration flow — HTML form, Node.js HTTP server, and MySQL database — entirely on your own machine before deploying anywhere. This guide walks through every step from cloning the repository to opening the app in your browser.

System Requirements

Make sure the following are installed before you begin:
RequirementMinimum Version
Node.jsv14+
npmv6+
MySQL or MariaDBMySQL 5.7+ / MariaDB 10.3+
1

Clone the Repository

Download the project source to your local machine and navigate into the project directory.
git clone https://github.com/brandonvergara1220-del/Mini-Proyecto-Backend-NodeJS.git && cd Mini-Proyecto-Backend-NodeJS
2

Install Dependencies

Install the required Node.js packages. The only production dependency is mysql2.
npm install
After this step you will see a node_modules folder and a package-lock.json file in the project root.
3

Set Up MySQL

Start your MySQL server and create the database that conexion.js expects. The connection is configured for localhost, user root, and an empty password — adjust these values in conexion.js if your setup differs.
CREATE DATABASE IF NOT EXISTS ejemploformulario;

USE ejemploformulario;

CREATE TABLE IF NOT EXISTS usuarios (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  nombre      VARCHAR(100) NOT NULL,
  apellido    VARCHAR(100) NOT NULL,
  created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
You can run these statements in the MySQL CLI, MySQL Workbench, or any compatible client.
4

Implement server.js

The server.js file in the repository is intentionally left empty — implementing it is the student’s task for this activity (GA7-220501096-AA2-EV02). The server must:
  • Serve index.html on GET /
  • Accept form submissions on POST /recuperardatos
  • Insert the submitted nombre and apellido values into the usuarios table using the exported conexion from conexion.js
Below is a minimal reference implementation that satisfies those requirements:
const http = require('http');
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const conexion = require('./conexion');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/') {
    const filePath = path.join(__dirname, 'index.html');
    fs.readFile(filePath, (err, data) => {
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end(data);
    });
  } else if (req.method === 'POST' && req.url === '/recuperardatos') {
    let body = '';
    req.on('data', chunk => { body += chunk.toString(); });
    req.on('end', () => {
      const { nombre, apellido } = querystring.parse(body);
      const sql = 'INSERT INTO usuarios (nombre, apellido) VALUES (?, ?)';
      conexion.query(sql, [nombre, apellido], (err, result) => {
        if (err) throw err;
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end(`Registered: ${nombre} ${apellido}`);
      });
    });
  }
});

server.listen(3000, () => console.log('Server running on http://localhost:3000'));
The code above is a reference implementation provided for learning purposes. The actual server.js file in the repository is empty — you are expected to write your own solution as part of the SENA ADSO activity.
5

Start the Server

Use the npm script defined in package.json to launch the server:
npm start
This runs node server.js. A successful start prints:
Conexión exitosa a la base de datos
Server running on http://localhost:3000
6

Open the App in Your Browser

Navigate to http://localhost:3000 in any modern browser. You should see the registration form defined in index.html. Fill in the Nombre and Apellido fields and click Enviar to submit a record to the usuarios table.

Troubleshooting

The error Error: connect ECONNREFUSED 127.0.0.1:3306 means the Node.js process cannot reach MySQL. Common causes and fixes:
  • MySQL is not running. Start the service with sudo service mysql start (Linux) or via the MySQL tray icon / System Preferences (macOS/Windows).
  • Wrong credentials. Open conexion.js and verify host, user, and password match your local MySQL configuration.
  • Database does not exist. Run CREATE DATABASE IF NOT EXISTS ejemploformulario; in the MySQL CLI and restart the server.
The error Error: listen EADDRINUSE: address already in use :::3000 means another process is occupying port 3000.Find and stop the process:
# macOS / Linux
lsof -i :3000
kill -9 <PID>

# Windows (PowerShell)
netstat -ano | findstr :3000
taskkill /PID <PID> /F
Alternatively, change the port number in server.js (e.g., 3001) and restart.
The error Cannot find module 'mysql2' means the dependency was not installed. Run:
npm install
If the error persists, delete node_modules and package-lock.json and try again:
rm -rf node_modules package-lock.json
npm install

Build docs developers (and LLMs) love