Use this file to discover all available pages before exploring further.
Once the base registration flow is working, the project becomes a solid foundation for practising more advanced Node.js and MySQL concepts. This guide shows concrete, incremental changes — adding a new field, exposing a listing route, and validating input — so you can grow the application one step at a time without reaching for a framework.
The existing form collects nombre and apellido. Adding an email field requires three coordinated changes: the HTML form, the database schema, and the server-side INSERT query.
A listing route lets you verify that records are being saved and is useful for debugging during development. Add a new branch to the request handler inside server.js:
} else if (req.method === 'GET' && req.url === '/usuarios') { const sql = 'SELECT id, nombre, apellido FROM usuarios ORDER BY created_at DESC'; conexion.query(sql, (err, rows) => { if (err) throw err; res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(rows)); });}
Before executing the INSERT, check that the required fields are present and non-empty. Add this guard block right after parsing the request body:
req.on('end', () => { const { nombre, apellido } = querystring.parse(body); // Validation: both fields must be non-empty strings if (!nombre || !apellido || nombre.trim() === '' || apellido.trim() === '') { res.writeHead(400, { 'Content-Type': 'text/plain' }); res.end('Error: nombre and apellido are required.'); return; } const sql = 'INSERT INTO usuarios (nombre, apellido) VALUES (?, ?)'; conexion.query(sql, [nombre.trim(), apellido.trim()], (err, result) => { if (err) throw err; res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end(`Registered: ${nombre.trim()} ${apellido.trim()}`); });});
This pattern — validate, then persist — keeps the database clean and makes errors visible to the client with a proper HTTP 400 status code rather than a silent failure or unhandled exception.
As the number of routes and validation rules grows, managing everything inside a single http.createServer callback becomes unwieldy. Consider migrating to Express.js (npm install express) to gain a clean router API, middleware support, and body-parsing utilities out of the box. The conexion.js module works without modification in an Express app — simply require('./conexion') wherever you need it.