This project aims to provide an easier boilerplate to start an simple Rest API on TypeScript and Swagger implemented over Clean Archicteture concepts.
Built with TypeScript for type safety and enhanced developer experience. Utilizes Express.js for robust and scalable routing. Implements Swagger for clear and interactive API documentation. Adheres to Clean Architecture principles for modularity and testability.
To get started with TS-Clean-Architecture-Rest-APi-Boilerplate, follow these steps:
Clone the repository:
git clone github.com/renatosantosti/TS-Clean-Architecture-Rest-APi-Boilerplate.git
Install dependencies:
npm install
Start the development server:
npm run dev
If we want learn to implements this project by yourself I would recommend you to follow these step below and also you should take time to read recommended references.
mkdir node-ts-express-jwt-swagger-api-boilerplate
cd express-typescript
npm init -y
Install TypeScript:
npm i -D typescript
Add tsconfig.json file
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"strict": true,
"esModuleInterop": true
}
}
Install Express as dependency and type definitions of node and express as development dependencies.
npm i -S express
npm i -D @types/express @types/node
Adding Express Middlewares
npm i -S morgan
npm i -D @types/morgan
npm i -S joi
Install NodeMon
npm i -D ts-node nodemon
Install Swagger and its dependencies
npm i -S tsoa swagger-ui-express
npm i -D @types/swagger-ui-express concurrently
to that last dependencies just add following settings on tsconfig.json
{
"compilerOptions": {
...
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
To encrypt password
npm i -S bcrypt jsonwebtoken
npm i -D @types/bcrypt @types/jsonwebtoken
Finally change package.json to this:
"scripts": {
"start": "node dist/index.js",
"build": "tsc",
"dev": "nodemon"
},
Throught the node-ts-express-jwt-swagger-api-boilerplate is very easy to install and deploy in a Docker container.
By default, the Docker will expose port 8080, so change this within the Dockerfile if necessary. When ready, simply use the Dockerfile to build the image.
cd node-ts-express-jwt-swagger-api-boilerplate
docker build --no-cache -t node-ts-express-jwt-swagger-api-boilerplate-image .
This will create the boilerplate image and pull in the necessary dependencies.
Once done, run the Docker image and map the port to whatever you wish on your host. In this example, we simply map port 8000 of the host to port 8080 of the Docker (or whatever port was exposed in the Dockerfile):
docker run -d -p 8080:8000 --restart=always --cap-add=SYS_ADMIN --name=node-ts-express-jwt-swagger-api-boilerplate-container node-ts-express-jwt-swagger-api-boilerplate-image
Verify the deployment by navigating to your server address in your preferred browser and voila!
http://127.0.0.1:8080/api-docs/
docker logs -f node-ts-express-jwt-swagger-api-boilerplate-container
docker exec -it node-ts-express-jwt-swagger-api-boilerplate-container sh
netstat -lntu
Used Projects | Link |
---|---|
TypeScript | https://www.typescriptlang.org |
Express | https://expressjs.com |
Swagger | https://swagger.io |
Swagger-JsDoc | https://github.com/Surnet/swagger-jsdoc |
TSoa | https://github.com/lukeautry/tsoa |
Morgan | https://expressjs.com/en/resources/middleware/morgan.html |
Docker | https://docs.docker.com |
Additionally read about JSDoc 3 is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor: https://swagger.io/specification
And also the OpenAPI Specification (OAS) defines a standard, language-agnostic interface to HTTP APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic: https://swagger.io/specification
https://dev.to/juliecherner/authentication-with-jwt-tokens-in-typescript-with-express-3gb1
MIT
Free Software, Hell Yeah!