Structure for node.js projects involving typescript
Hello, my name is Lucas Bruno, and I am providing this framework, which has been very helpful to me during the development of REST APIs using Node.js and TypeScript. Hope you enjoy, any feedback and pull request is very welcome!
In order to use this project, you must have the following requirements:
- NODE
- NPM
Project structure:
βββ src
β βββ api ---> The Folder to handler API layers
β β βββ controllers ---> Input and Output of the application, along with basic request validations.
β β β
β β βββ middlewares ---> Request Interceptors
β β β
β β βββ routes ---> App route settings
β β
β βββ config ---> Application Settings, Logging, Environment, Express middleware ...
β β
β βββ interfaces ---> Implementation of interfaces for development assistance
β β
β βββ models ---> Models, Entities and Schemas for manipulation and persistence.
β β
β βββ persistence ---> Definitions and database instances.
β β
β βββ services ---> It gathers business rules and external manipulations (Database, other api's ...)
β
βββ tests ---> Unit Tests
So you can run your project in your environment, follow these steps:
Clone the project in your preferred folder.
cd nodejs-rest-api
npm install
npm run dev
And ready! The application will be running on the port defined in src/server.ts
This application has unit tests using the Jest framework.
To run unit tests:
npm run test
To run unit tests with coverage:
npm run coverage
So that we can write cleaner and more beautiful code, we can adopt linear frameworks like Eslint.
npm run lint
If you want to enjoy the application in production, you can use the Docker.
For this, the project has the Dockerfile configuration file, so that it can generate the project image.
docker build --name nodejs-api .
docker run -d -p 8080:8080 nodejs-api
- Express - The web framework used
- TypeScript - Is a Typed superset of Javascript
- Mongoose - Elegant mongodb object modeling for node.js
- Jest - Is a delightful JavaScript Testing Framework
- Eslint - The pluggable linting utility for JavaScript
- EditorConfig - Helps maintain consistent coding styles
- Santiq - Bulletproof node.js project architecture
- RocketSeat - Project code produced during TypeScript video on NodeJS