diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 0223e70..c4b74d7 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - node-version: [14.x, 16.x, 18.x] + node-version: [20.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: diff --git a/dbscripts/mysql.sql b/dbscripts/mysql.sql index 56c6f9e..ca39e16 100644 --- a/dbscripts/mysql.sql +++ b/dbscripts/mysql.sql @@ -12,7 +12,7 @@ CREATE TABLE `usuarios` ( `updatedAt` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) -) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; @@ -29,4 +29,4 @@ CREATE TABLE `usuarios` ( `updatedAt` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) -) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file diff --git a/src/controllers/auth/auth.ts b/src/controllers/auth/auth.ts index bb30e07..ea93782 100644 --- a/src/controllers/auth/auth.ts +++ b/src/controllers/auth/auth.ts @@ -1,8 +1,8 @@ import { Request, Response } from 'express'; +import bcrypt from 'bcrypt'; import { SuccessResponse } from '../../core/ApiResponse'; import { BadRequestError } from '../../core/ApiError'; import * as usuarioService from "../../services/usuarioService"; -import bcrypt from 'bcrypt'; import { createTokens } from '../../utils/utils'; const register = async (req: Request, res: Response) => { @@ -42,7 +42,7 @@ const login = async (req: Request, res: Response) => { maxAge: 1000 * 60 * 60 * 24 * 7, }); - new SuccessResponse(res, "Inicio de sesion exitoso", { usuario: usuario, accessToken: accessToken }) + new SuccessResponse(res, "Inicio de sesion exitoso", { usuario, accessToken }) } export default { diff --git a/src/controllers/test/test.ts b/src/controllers/test/test.ts new file mode 100644 index 0000000..b8d97bd --- /dev/null +++ b/src/controllers/test/test.ts @@ -0,0 +1,8 @@ +import { Request, Response } from 'express'; +import { SuccessMsgResponse } from '../../core/ApiResponse'; + +const test = async (req: Request, res: Response) => { + new SuccessMsgResponse(res, "Respuesta de prueba!"); +} + +export default { test } \ No newline at end of file diff --git a/src/loaders/express.ts b/src/loaders/express.ts index b8a4ec3..7429a62 100644 --- a/src/loaders/express.ts +++ b/src/loaders/express.ts @@ -1,14 +1,13 @@ import express from "express"; import { Express } from "express"; -import routes from "../routes/index"; +import routes from "../routes"; + import cors from "cors"; import cookieParser from "cookie-parser"; +import helmet from "helmet"; import { errorMiddleware } from "../middlewares/errorMiddleware"; import { requestLogger } from "../middlewares/requestLoggerMiddleware"; import { notFoundMiddleware } from "../middlewares/404Middleware"; -import helmet from "helmet"; -import asyncErrorHandler from "../../src/utils/asyncErrorHandler"; -import authMiddleware from "../../src/middlewares/authMiddleware"; const expressLoader = async ({ app }: { app: Express }) => { app.use(express.json()); @@ -24,13 +23,13 @@ const expressLoader = async ({ app }: { app: Express }) => { app.use(requestLogger); app.use(helmet()); - + //Rutas de la applicación app.use("/api", routes); - + //Middleware para manejar rutas no encontradas app.use("*", notFoundMiddleware); - app.use(asyncErrorHandler(authMiddleware)) + //Middleware para manejar errores app.use(errorMiddleware); }; diff --git a/src/middlewares/404Middleware.ts b/src/middlewares/404Middleware.ts index 5741ddd..f7954d0 100644 --- a/src/middlewares/404Middleware.ts +++ b/src/middlewares/404Middleware.ts @@ -2,6 +2,6 @@ import { Request, Response, NextFunction } from 'express'; import { NotFoundError } from '../core/ApiError' export const notFoundMiddleware = (req: Request, res: Response, next: NextFunction) => { - const error = new NotFoundError(`Not Found - ${req.originalUrl}`); + const error = new NotFoundError(`Not Found - ${req.method} - ${req.originalUrl}`); next(error); }; diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 722426b..43c86e5 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -1,4 +1,4 @@ -import { Router, Request, Response, NextFunction } from "express"; +import { Router } from "express"; import asyncErrorHandler from "../utils/asyncErrorHandler"; import { validateRequest } from "../middlewares/validateRequest"; import authController from "../controllers/auth/auth"; diff --git a/src/routes/index.ts b/src/routes/index.ts index d883c0f..4debac0 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,12 +1,16 @@ import express from "express"; -import auth from "./auth"; -import authMiddleware from "../middlewares/authMiddleware"; -import asyncErrorHandler from "../utils/asyncErrorHandler"; +import testRoutes from "./test" +import authRoutes from "./auth"; +import asyncErrorHandler from "../../src/utils/asyncErrorHandler"; +import authMiddleware from "../../src/middlewares/authMiddleware"; + const app = express(); // Rutas no protegidas por middleware de autenticación -app.use("/auth", auth); +app.use("/auth", authRoutes); // Rutas protegidas por middleware de autenticación +app.get("/test", asyncErrorHandler(authMiddleware), testRoutes); + export default app; diff --git a/src/routes/test.ts b/src/routes/test.ts new file mode 100644 index 0000000..520c45e --- /dev/null +++ b/src/routes/test.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import asyncErrorHandler from "../utils/asyncErrorHandler"; +import testController from "../controllers/test/test"; + +const app = Router(); + +app.get("/", asyncErrorHandler(testController.test)); + + +export default app; \ No newline at end of file