This is a NodeJS-Express application written in Typescript. It exposes a single RestAPI that allows to retrieve data from a connected MongoDB instance. It uses StandardJS code style.
These are the main modules used in the app:
Mongoose
as driver to connect to a MongoDB instancewinston
to manage logexpress-validator
to validate request datadotenv
andconfig
to load environment variables and manage different environmentsjest
andsupertest
for Unit Tests and Integration Tests
It implements a (simplified and hopefully not over engineered) basic version of a 3-tier architecture: for each component (usually an exposed API path), there are a Controller (manages request and response and the express client itself), Service (manages the business logic), Repository (manages the retrieve and store operations with external data sources, i.e. database, file storage, ecc). The 3-tier architecture is based on Dependency Injection and Inversion of Control, in order to make each tier independent, reusable, easily-swappable, easily-testable-mockable. In this case, since it is a basic application, with simple specs, the Service tier has not been implemented in order to avoid over-engineering.
- First install the dependencies
npm install
-
Then complete the code style configuration: the project uses
standard
code style. All the necessary modules are installed throughnpm install
, but run the following steps for a complete setup:- Install
standard
extension for your IDE: see here for instructions - Activate auto fix/lint on save on the IDE
standard
extension: if you are using Visual Studio Code this setting is already present (see.vscode/settings.json
)
- Install
Now everything is ready to code and to run the application.
These are all the available ways to run the application
- Run locally without ts transpilation, it starts with debugger on port 9949:
npm run dev
- Run locally in watch mode, it starts with debugger on port 9949:
npm run dev:watch
- Run locally with ts transpilation:
npm run start
- Run locally with ts transpilation, it starts with debugger on port 9949:
npm run start:debug
These are the ways to run tests locally
- Run tests, with coverage
npm run test
- Run tests in watch mode
npm run test:watch
These are the instructions to deploy the application to Heroku.
-
Create an Heroku account: see here
-
Install the Heroku CLI: see here
-
Open a terminal in the project root and run this command to login into Heroku from CLI:
heroku login
-
Create an Heroku app (region parameter is optional)
heroku apps:create example-app --region eu
This command will automatically set a new remote in the local git repository that points to the heroku remote repository, it also returns the git remote repository path and the public exposed endpoint of the application
-
Deploy the app to the Heroku remote with this command:
git push heroku main
-
Open the app with this command:
heroku open