The Express-Demo application is a Node.js/Express.js server written in TypeScript, aimed at testing and demonstrating several integrations including OpenAPI, SQLite3, MongoDB, and various middlewares. This application showcases a structured approach to handling functionalities like logging, authentication, and database operations in a Node.js environment.
- OpenAPI Integration: The application adheres to OpenAPI specifications, aiding in the design, building, and interaction with APIs. It also provides a Swagger UI for a user-friendly API interaction.
- Middleware Utilization: Implements various middlewares for logging, authentication (JWT), and error handling.
- Database Strategies: Incorporates a strategy pattern for interchangeable database operations. Supports MongoDB, SQLite, and a no-database option for testing or lightweight purposes.
- Database Operations: Provides structured means to create, read, update, and delete (CRUD) user data.
- Authentication: Utilizes JSON Web Tokens (JWT) for authentication.
- Error Handling: Includes middleware for capturing and responding to errors in a structured format.
- Testing Tokens: Provides a script to generate JWTs for testing.
Ensure you have the following installed on your machine:
- Node.js
- TypeScript
- A supported database (MongoDB/SQLite) or none for the no-database strategy.
- Clone the repository to your local machine.
- Run
npm install
to install all necessary dependencies listed in thepackage.json
file. - Set the desired
DB_STRATEGY
in your environment variables or in a.env
file. Options are 'mongodb', 'sqlite', or 'nodb'. - If using a database, ensure it's running and accessible.
- Use
npm run start
to start the application. This will compile the TypeScript files and start the server athttp://localhost:3000
. - Use
npm run dev
for development mode with hot-reload. - For generating a JWT for testing, run
npm run get:token
.
Access the Swagger UI at http://localhost:3000/api-docs
to interact with the API in a user-friendly manner.
Various scripts are provided for convenience:
start
: Starts the application.build
: Compiles TypeScript files.get:token
: Generates a JWT for testing purposes.dev
: Starts the application in development mode with hot-reload.
.
├── LICENSE
├── README.md
├── node_modules
├── nodemon.json
├── package.json
├── src
│ ├── assets
│ │ └── favicon.ico
│ ├── authentication
│ │ └── jwt.strategy.ts
│ ├── bootstrap.ts
│ ├── controllers
│ │ ├── home
│ │ ├── index.ts
│ │ └── users
│ ├── index.ts
│ ├── middlewares
│ │ ├── authentication
│ │ ├── index.ts
│ │ ├── logging
│ │ └── openapi
│ ├── persistence
│ │ ├── db
│ │ ├── index.ts
│ │ ├── models
│ │ ├── strategy
│ │ └── vendor
│ └── test
│ └── get_token.ts
└── tsconfig.json
There are known issues related to openapi3-ts
version conflicts which require certain hacks as documented in the source code. These hacks will be removed once the underlying libraries resolve the conflicts.