TheDocumentation 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.
conexion.js module is the sole database layer in Mini Proyecto Backend NodeJS. Rather than opening a new connection every time a query runs, it creates one persistent connection at startup, verifies it is healthy, and then exports it so any other file — most importantly server.js — can call .query() without needing to know the database credentials or connection details.
Full Source
Configuration Fields
The object passed tomysql.createConnection() tells the driver exactly where to find MySQL and how to authenticate:
| Field | Value | Purpose |
|---|---|---|
host | "localhost" | The hostname or IP address of the MySQL server. localhost means the database runs on the same machine as Node.js. |
database | "ejemploformulario" | The specific MySQL database (schema) to connect to. All queries run against this database unless an explicit schema is prefixed in the SQL. |
user | "root" | The MySQL username used to authenticate. root is the built-in superuser account on a local development server. |
password | "" | The password for the MySQL user. An empty string means no password is required — typical for a local XAMPP or WAMP development setup. |
The conexion.connect() Callback
After the connection object is created, .connect() physically opens the TCP socket to MySQL and authenticates. The callback receives a single error argument:
- If
erroris truthy — something went wrong (wrong credentials, MySQL not running, database does not exist, etc.). The module throws the error immediately, which crashes the process with a visible stack trace rather than failing silently later. - If
erroris falsy — the connection is live. Theconsole.logmessage"Conexión exitosa a la base de datos"is printed to the terminal as a confirmation that the server started correctly.
module.exports and require()
The final line — module.exports = conexion; — exports the already-connected mysql2 connection object using Node.js’s CommonJS module system (the "type": "commonjs" entry in package.json confirms this project uses CommonJS).
Any other file in the project can import the same live connection with a single require call:
require() caches modules after the first load, every call to require('./conexion') throughout the application returns the same connection object — no duplicate connections are opened.
Why
mysql2 and not mysql? The mysql2 package is the actively maintained successor to the original mysql npm package. It offers full compatibility with MySQL 8’s default caching_sha2_password authentication plugin, better performance through binary protocol support, and Promise-based APIs alongside the callback style used here. package.json pins it at ^3.22.5.