This is a Basic TypeScript REST API with all the basic tools you need to keep your codebase sparkling while it grows.
The
main
branch in this repository is protected. Pushes must be done to a feature branch and when status checks pass it can be merged into the main branch.
-
If you're using NVM to manage your Node installation/versions, run
nvm use
to use the Node version specified on.nvmrc
file. -
Copy the content of
.env.example
to.env
and customize the values of your settings. The .env file is registered in the .gitignore and should not be submitted to your code repository. -
This project uses Husky to run command on Git Hooks. Explore
.huskyrc.json
. It's a best practice to do not commit code that is broken and to not push code that lowers the test coverage below the minimum acceptable for the project. -
Nodemon is only used in development, and only exists as devDependency. Avoid using
npm run start
in production, but instead, runnode ./dist/index.js
. -
Prettier
is used to format your code. Customize your settings in the.prettierrc
file. -
Jest
is used as a testing framework (and assertion library). Feel free to replace to Mocha/Chai, Tape, AVA, or whatever is your cup of tea. -
ESLint
is used as our linter of choice since when TS Lint was deprecated. -
Explore the
scripts
section in thepackage.json
to view all the shortcuts you have at your disposal. -
Have fun!
Starts the application in development using nodemon
and ts-node
for hot reloading.
Starts the application in development mode using nodemon
without hot reloading.
Whenever you add breakpoints in the app and wants to use Chrome Dev Tools to inspect your code in execution time, this is what you need.
Expose your app from your computer to the internet. Make sure the por specified in the script (3010 in this case), matches the port you have set up in the .env
file. This is useul when working with 3rd party tools that need a callback URL.
Builds the app in the /dist
folder. This is the code to be used in production.
When you're working on implementation details that are covered by unit tets, you often want to test the result of your changes on every "save". This command is your best friend.
Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command:
docker build -t <your username>/api-zero .
Your image will now be listed by Docker:
docker images
Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built:
docker run -p 3010:8080 -d <your username>/api-zero
Print the output of your app:
Get container ID
docker ps
Print app output
docker logs <container id>
If you need to go inside the container you can use the exec command:
Enter the container:
docker exec -it <container id> /bin/bash
PRO TIP (1): Configure your VS Code to format your code on save: Code -> Preferences -> Settings (for Mac) Search for "Default Formatter". In the dropdown, prettier will show as esbenp.prettier-vscode.
PRO TIP (2): On VS Code, the Debug sidebar panel displays a few options loaded from the .vscode/launch.json
. When you want to run only 1 specific *.spec.ts
file, open this file and select Jest Debug File
. When you want to run all test files and stop on breakpoints, choose Jest Debug
.