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.

The 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

let mysql = require('mysql2');

// Conexión reutilizable a la base de datos del proyecto.
let conexion = mysql.createConnection({
  host: "localhost",
  database: "ejemploformulario",
  user: "root",
  password: ""
});

conexion.connect(function(error) {
  if (error) {
    throw error;
  } else {
    console.log("Conexión exitosa a la base de datos");
  }
});

module.exports = conexion;

Configuration Fields

The object passed to mysql.createConnection() tells the driver exactly where to find MySQL and how to authenticate:
FieldValuePurpose
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 error is 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 error is falsy — the connection is live. The console.log message "Conexión exitosa a la base de datos" is printed to the terminal as a confirmation that the server started correctly.
This fail-fast pattern is appropriate for a learning project: a connection problem surfaces immediately at startup rather than producing confusing errors on the first query.

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:
const conexion = require('./conexion');

// conexion is now the live mysql2 connection object
// Use conexion.query() to run SQL statements
Because 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.
Hardcoded credentials are a security risk. The user and password values are written directly in source code, which means they will be committed to version control and visible to anyone who reads the repository. For any project beyond local development, move credentials to environment variables:
let conexion = mysql.createConnection({
  host:     process.env.DB_HOST     || "localhost",
  database: process.env.DB_NAME     || "ejemploformulario",
  user:     process.env.DB_USER     || "root",
  password: process.env.DB_PASSWORD || ""
});
Store the real values in a .env file (and add .env to .gitignore) or inject them through your deployment environment.
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.

Build docs developers (and LLMs) love