/api_typescript_mysql

03. API REST básica empleando Node.js, Express.js, MySQL y Typescript.

Primary LanguageTypeScript

Basic API REST

Node.js, Express.js, MySQL, Typescript Basic REST API.

You can clone this repo as starter project for your Express, MySQL API server

Features and Functionalities 😃

  • Node, Express, Typescript, MySQL Basic REST API
  • CRUD Operations (A Controller for this)
  • SQL for database: Relational MySQL

Tech Stack 💻

Installation and Running App ⚡

1. Clone this repo by running the following command :-

 git clone https://github.com/norbeydanilo/api_typescript_mysql.git
 cd api_typescript_mysql

2. Install the required package :-

 npm install

3. Now run the npm command to start the project :-

 npm run dev

4. 🎉 Open postman and test the rest api on this url https://127.0.0.1:3000

Remember that the .env file must be created for the API to work.

You must run the script.sql script to create the database.

Additionally this project uses:

Follow the steps of the Steps presentation in the repository -

Swagger and Running

Debes tener instalado Swagger.

npm install swagger-jsdoc swagger-ui-express
npm install @types/swagger-ui-express @types/swagger-jsdoc --save-dev

Luego debes importarlo en el proyecto en app.ts:

import swaggerUi from 'swagger-ui-express';
import * as swaggerDocument from "./src/swagger.json";

Si te aparece el problema de Cannot find module './src/swagger.json es por la importación de un archivo JSON en TypeScript.

En TypeScript, por defecto, no se pueden importar archivos JSON directamente. Para solucionar este problema, debes habilitar la opción --resolveJsonModule en tu archivo de configuración de TypeScript (tsconfig.json).

Finalmente agrega la ruta para la documentación con Swagger.

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

Con esto, podrás acceder a la documentación de Swagger en la ruta /api-docs de tu aplicación.

Por favor, ten en cuenta que este es un ejemplo básico y puedes necesitar ajustarlo según tus necesidades. Por ejemplo, puedes querer agregar autenticación a la ruta de la documentación de Swagger, o puedes tener otros middlewares que necesiten ser configurados. Te recomiendo que consultes la documentación de swagger-ui-express para obtener más detalles.