Commit inicial
This commit is contained in:
commit
38f5722896
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
node_modules/
|
||||||
|
.env
|
||||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Iniciar app.js",
|
||||||
|
"program": "${workspaceFolder}/app.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
129
README.md
Normal file
129
README.md
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
# npm – Resumen rápido
|
||||||
|
|
||||||
|
## ¿Qué es npm?
|
||||||
|
|
||||||
|
**npm** (Node Package Manager) es la herramienta de gestión de paquetes y automatización de proyectos para Node.js.
|
||||||
|
|
||||||
|
Permite instalar librerías, ejecutar scripts y gestionar dependencias de forma automática. Todo se configura mediante un archivo central llamado `package.json`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Conceptos básicos
|
||||||
|
|
||||||
|
### `package.json`
|
||||||
|
|
||||||
|
Es el archivo principal donde se define:
|
||||||
|
|
||||||
|
- Información del proyecto (`name`, `version`, `description`)
|
||||||
|
- Dependencias (`dependencies` y `devDependencies`)
|
||||||
|
- Scripts de ejecución
|
||||||
|
- Configuración de compilación o herramientas adicionales
|
||||||
|
|
||||||
|
**Ejemplo básico:**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"name": "mi-proyecto",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Proyecto de ejemplo"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Dependencias
|
||||||
|
|
||||||
|
Las librerías necesarias para el proyecto se declaran en `package.json`:
|
||||||
|
|
||||||
|
- `"dependencies"` → necesarias para **ejecución**
|
||||||
|
- `"devDependencies"` → necesarias solo para **desarrollo**
|
||||||
|
|
||||||
|
**Ejemplo:**
|
||||||
|
|
||||||
|
```json
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^5.2.1",
|
||||||
|
"mysql2": "^3.19.1"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Para instalarlas se ejecuta:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
> npm descargará automáticamente las librerías en la carpeta `node_modules`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Scripts npm
|
||||||
|
|
||||||
|
Los scripts permiten automatizar tareas comunes, como ejecutar la aplicación, tests o compilaciones.
|
||||||
|
|
||||||
|
**Ejemplo en `package.json`:**
|
||||||
|
|
||||||
|
```json
|
||||||
|
"scripts": {
|
||||||
|
"start": "node app.js",
|
||||||
|
"dev": "node app.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Se ejecutan con:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run start # o simplemente: npm start
|
||||||
|
npm run dev # para desarrollo
|
||||||
|
npm run test # ejecutar tests
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Ciclo de vida / tareas comunes
|
||||||
|
|
||||||
|
| Comando | Función |
|
||||||
|
|---|---|
|
||||||
|
| `npm install` | Instala las dependencias |
|
||||||
|
| `npm start` | Ejecuta la aplicación |
|
||||||
|
| `npm run <script>` | Ejecuta un script definido en `package.json` |
|
||||||
|
| `npm test` | Ejecuta los tests definidos |
|
||||||
|
| `npm update` | Actualiza las dependencias |
|
||||||
|
| `npm uninstall` | Elimina un paquete |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Repositorios
|
||||||
|
|
||||||
|
npm descarga paquetes desde repositorios:
|
||||||
|
|
||||||
|
- **Local** → dentro de `node_modules` del proyecto
|
||||||
|
- **Remoto** → el registro público de npm ([https://www.npmjs.com/](https://www.npmjs.com/))
|
||||||
|
- **Privado** → repositorios internos o privados
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Estructura típica de un proyecto Node.js + Express
|
||||||
|
|
||||||
|
```
|
||||||
|
proyecto/
|
||||||
|
│
|
||||||
|
├── package.json
|
||||||
|
├── package-lock.json
|
||||||
|
├── node_modules/
|
||||||
|
└── src/
|
||||||
|
├── app.js
|
||||||
|
├── controllers/
|
||||||
|
├── services/
|
||||||
|
├── dao/
|
||||||
|
└── models/
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 💡 Idea clave
|
||||||
|
|
||||||
|
> npm sigue el principio **"Convention over Configuration"** de manera ligera:
|
||||||
|
> si respetas la estructura estándar de Node.js y defines tus scripts,
|
||||||
|
> necesitas muy poca configuración para ejecutar tu proyecto.
|
||||||
12
app.js
Normal file
12
app.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// app.js
|
||||||
|
const express = require('express');
|
||||||
|
const app = express();
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
const continenteController = require('./controllers/ContinenteController');
|
||||||
|
app.use('/api/continentes', continenteController);
|
||||||
|
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Servidor escuchando en http://localhost:${port}`);
|
||||||
|
});
|
||||||
41
controllers/ContinenteController.js
Normal file
41
controllers/ContinenteController.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// controllers/ContinenteController.js
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const continenteService = require('../services/ContinenteService');
|
||||||
|
|
||||||
|
// GET /api/continentes - listar todos los continentes
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const lista = await continenteService.obtenerListaContinentes();
|
||||||
|
res.json(lista);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).send('Error en el servidor');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /api/continentes/codigo/:codigo - obtener continente por código
|
||||||
|
router.get('/codigo/:codigo', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const continente = await continenteService.obtenerContinentePorCodigo(req.params.codigo);
|
||||||
|
if (!continente) return res.status(404).send('Continente no encontrado');
|
||||||
|
res.json(continente);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).send('Error en el servidor');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /api/continentes/nombre/:nombre - obtener continente por nombre
|
||||||
|
router.get('/nombre/:nombre', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const resultados = await continenteService.obtenerContinentePorNombre(req.params.nombre);
|
||||||
|
if (resultados.length === 0) return res.status(404).send('Continente no encontrado');
|
||||||
|
res.json(resultados);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).send('Error en el servidor');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
19
dao/IContinenteDAO.js
Normal file
19
dao/IContinenteDAO.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* Interfaz DAO para la entidad Continente.
|
||||||
|
* Define los métodos que la implementación debe ofrecer.
|
||||||
|
*/
|
||||||
|
class IContinenteDAO {
|
||||||
|
listarTodos() {
|
||||||
|
throw new Error('Método listarTodos() no implementado');
|
||||||
|
}
|
||||||
|
|
||||||
|
buscarPorCodigo(codigo) {
|
||||||
|
throw new Error('Método buscarPorCodigo() no implementado');
|
||||||
|
}
|
||||||
|
|
||||||
|
buscarPorNombre(nombre) {
|
||||||
|
throw new Error('Método buscarPorNombre() no implementado');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = IContinenteDAO;
|
||||||
34
dao/impl/ContinenteDAOImpl.js
Normal file
34
dao/impl/ContinenteDAOImpl.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
const IContinenteDAO = require('../IContinenteDAO');
|
||||||
|
const pool = require('../../gestores/gestorDB');
|
||||||
|
const Continente = require('../../models/Continente');
|
||||||
|
|
||||||
|
class ContinenteDAOImpl extends IContinenteDAO {
|
||||||
|
|
||||||
|
// Listar todos los continentes
|
||||||
|
async listarTodos() {
|
||||||
|
const [rows] = await pool.query('SELECT codigo, nombre_continente AS nombre FROM T_CONTINENTE');
|
||||||
|
return rows.map(r => new Continente(r.codigo, r.nombre));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buscar por código
|
||||||
|
async buscarPorCodigo(codigo) {
|
||||||
|
const [rows] = await pool.query(
|
||||||
|
'SELECT codigo, nombre_continente AS nombre FROM T_CONTINENTE WHERE codigo = ?',
|
||||||
|
[codigo]
|
||||||
|
);
|
||||||
|
if (rows.length === 0) return null;
|
||||||
|
const r = rows[0];
|
||||||
|
return new Continente(r.codigo, r.nombre);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buscar por nombre
|
||||||
|
async buscarPorNombre(nombre) {
|
||||||
|
const [rows] = await pool.query(
|
||||||
|
'SELECT codigo, nombre_continente AS nombre FROM T_CONTINENTE WHERE nombre_continente LIKE ?',
|
||||||
|
[`%${nombre}%`]
|
||||||
|
);
|
||||||
|
return rows.map(r => new Continente(r.codigo, r.nombre));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ContinenteDAOImpl;
|
||||||
14
gestores/gestorDB.js
Normal file
14
gestores/gestorDB.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// gestores/gestorDB.js
|
||||||
|
const mysql = require('mysql2/promise');
|
||||||
|
|
||||||
|
const pool = mysql.createPool({
|
||||||
|
host: '192.168.1.36', // Host de MySQL
|
||||||
|
user: 'root', // Usuario MySQL
|
||||||
|
password: 'mysql_123', // Contraseña
|
||||||
|
database: 'mapa_mundi', // Nombre de la base de datos
|
||||||
|
waitForConnections: true,
|
||||||
|
connectionLimit: 10,
|
||||||
|
queueLimit: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = pool;
|
||||||
13
models/Continente.js
Normal file
13
models/Continente.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// models/Continente.js
|
||||||
|
class Continente {
|
||||||
|
constructor(codigo, nombre) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return `Continente [codigo=${this.codigo}, nombre=${this.nombre}]`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Continente;
|
||||||
1453
package-lock.json
generated
Normal file
1453
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
package.json
Normal file
18
package.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "aaee_ra3_proy4_node-express",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node app.js",
|
||||||
|
"dev": "node app.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^5.2.1",
|
||||||
|
"mysql2": "^3.19.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
24
services/ContinenteService.js
Normal file
24
services/ContinenteService.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
const ContinenteDAOImpl = require('../dao/impl/ContinenteDAOImpl');
|
||||||
|
|
||||||
|
class ContinenteService {
|
||||||
|
constructor() {
|
||||||
|
this.dao = new ContinenteDAOImpl(); // instancia del DAO
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtener todos los continentes
|
||||||
|
async obtenerListaContinentes() {
|
||||||
|
return await this.dao.listarTodos();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtener continente por código
|
||||||
|
async obtenerContinentePorCodigo(codigo) {
|
||||||
|
return await this.dao.buscarPorCodigo(codigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtener continente por nombre (puede devolver varios)
|
||||||
|
async obtenerContinentePorNombre(nombre) {
|
||||||
|
return await this.dao.buscarPorNombre(nombre);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = new ContinenteService();
|
||||||
Loading…
Reference in New Issue
Block a user