If you found this project helpful, please consider supporting me by buying me a coffee! Your support will help me to keep creating more useful content and improving this project.
This template is intended to be used as single service in a REST multi-service application using Cloud Pub/Sub as message broker
use in local with Skaffold and in cloud with GKE
To know more about how to implement GKE and run with Skaffold please refer to this folder:
./infra
The application uses express as framework and is configured with the following features:
ECMA2022features enabledDotenvLoad environment variables from .env fileEslintCode quality toolPrettierto prettify the codeMongoDBready to go configuration with mongooseMySQLready to go configuration with mysql2CORSfeature enabledRBAClogic to authorize people with specific roles to use the endpoints.Passportlogic to add an authentication layer if neeeded.Sparkpostemail service support with sparkpost.Error Handlingerrors custom middleware and helpers globally configuredMulterFile uploading configured to use in routes as middlewareGoogle Cloud Storagemiddleware configured to use Google Cloud Storage as upload bucketGoogle Cloud Pub/Subpub/sub support for event driven events addedAxiosglobally configured in./src/utils/api.utils.jsSwaggerdocumentation reachable athttp://localhost:3000/api/v1/docsJestTesting tool supportLoggerLogging support with WinstonDockerready configuration with multi-stage optionTerraformready configuration to instantiate infrastracture in GCPAgendaready to emit events through agenda jobsBest practicesin naming files
-
App entry point is located in
./src/index.ts -
Server config entrypoint is located in
./src/bin/server.ts -
Prettier config is located at
./.prettierrc.js -
Eslint config is located at
./.eslintrc -
Sparkpost service support is located at
./src/services/email/sparkport.service.ts- You can define your own email services in this file
-
Mongo config is located at
./src/config/mongodb.config.ts -
MYSQL config is located at
./src/config/mysql.config.ts -
Error Handling middleware is located at
./src/middlewares/errorHandler.middleware.ts- You can configure as many errors you need in
./src/errors/
- You can configure as many errors you need in
-
Multer middleware is located at
./src/middlewares/upload.middleware.ts- If you want to use Google Cloud Storage as upload bucket follow instructions at
./src/config/gcloud/README.md
- If you want to use Google Cloud Storage as upload bucket follow instructions at
-
RBAC logic middleware is located at
./src/middlewares/verifyApiRights.middleware.ts -
Swagger config file is located at
./src/api/swagger/swagger.route.js- Swagger routes are defined in
./src/api/swagger/swagger.route.ts
- Swagger routes are defined in
-
Docker config is located at
./Dockerfile -
Pub/Sub service is located at
./src/services/pubsub/pub-sub.service.js
infra/
- For more information about the k8s configuration please check the README file
k8s- folder contains all production kubernetes manifestsk8s-dev- folder contains all development kubernetes manifests to run with skaffoldscripts- older contains all script related to the creation of a cluster or running skaffold or secret creation
src/
api/- containing all api logic with model, services, controller and routesbin/- server configuration folderconfig/- this folder contains all the configs file (database, passport, etc...)constants/- this folder contains all the global constantslogs/- the logger file will be stored herehelpers/- some helpers func i.e. an error helper that returns json everytime an error comes inmiddlewares/- here you can find all the custom middlewaresservices/- here we store all the services; i.e. here we define methods to manipulate a db model entitytests/- here we store all the jest testutils/- containing some utils function to be reused in the code (i.e. axios global configuration)
Copy the .env.example to .env. Be sure to fill all the global variables. Alternatively you can use the script generate-env.sh in the scripts folder. This script will generate a .env.test.local and you can copy this file to .env
cp env.example .envThen replace:
MONGO_URIstring with your Mongo connectionMONGO_URI_TESTstring with your Mongo Test connection
MYSQL_HOST_STAGEstring with your mysql host nameMYSQL_USER_STAGEstring with your mysql usernameMYSQL_PASSWORD_STAGEstring with your mysql password nameMYSQL_DB_STAGEstring with your mysql db nameMYSQL_SOCKET_STAGEstring with your mysql socket name
GOOGLE_APPLICATION_CREDENTIALSpath with yoursGOOGLE_PROJECT_IDwith yoursSENDGRID_API_KEYwith yoursSENDGRID_SENDER_EMAILwith yours
In order to Google Cloud Storage works follow instructions located in ./src/config/gcloud/README.md
To get started with this repo npm install in the root folder
npm installTo getting started with a dev environment. Here we use nodemon and babel-node to restart the server asa we change something
npm run start:devTo compile the code and create a production build
npm run compileThis command will create a build in the root directory
To start with a production ready build you can run this command
# This set the NODE_ENV to production, npm-run-all, create a build and run the server command
npm run startIf you have a build and you want to node the build you can run
# This command launch the node instance inside the ./build/bin/server
npm run serverIf you use Docker and wanna dockerize the app you can run the command
docker build -t <dockerhubusername>/<docker-image-name>:<tag> .then
docker run --name <docker-process-name> -d - p 3000:3000 <dockerhubusername>/<docker-image-name>:<tag>