Archetype for NodeJS with typescript applications
See version file at project's root folder.
See LICENSE file at project's root folder.
- Fabio Marciano
- Typescript support
- Jest for tests
- Coverage (via jest)
- Editor config
- Prettier
- ESLint
- Docker / Docker Compose
- Hot reload in development environment
- Dotenv (environment variables)
- Nunjucks (template engine)
- Static public folder
The following available services are described at docker-compose.yml file. It uses some envirnment variables from .env file.
- NodeJS server
- PostgreSQL relational database
Important: The .env file must be considered only in development environment.
The NodeJS service is an NodeJS/Express/Alpine server.
The following environment variables are available at .env file.
Important:
- The .env file must be considered only in development environment.
Name | Description | Default Value |
---|---|---|
PORT | The application port | 3000 |
NODE_ENV | The NodeJS environment's current name | dev |
API_BASE | The application router base path | / |
PostgreSQL is the most advanced relational open database. The service uses some environment variables in order to setup the database.
It's possible to use SQL script files in order to create new databases, table schemas, initial inserts and more.
Before run the docker compose build command for the very first time, it's possible write SQL scripts under the cfg/postgres
folder. The scripts will be executed using alphabetical order, so it's a good pratice to rename the files using a numbered prefix.
Example:
+- .
|- ..
+- cfg
+- postgres
+- 001-1st-file-to-execute.sql
|- 002-2nd-file-to-execute.sql
+- 003-3rd-file-to-execute.sql
Name | Description | Default Value |
---|---|---|
POSTGRES_HOST | Postgres database host | 0.0.0.0 |
POSTGRES_PORT | Postgres database port | 5432 |
POSTGRES_DB | Postgres database name | postgres |
POSTGRES_USER | Postgres database user name | postgres |
POSTGRES_PASSWORD | Postgres database user password | postgres |
To access the PostgreSQL container, use the followind command:
$ docker exec -it <CONTAINER_NAME> psql -U <POSTGRES_USER>
To access via PostgreSQL command line client (requires to install postgresql-client
application) use the following command:
$ psql -h <POSTGRES_HOST> -p <POSTGRES_PORT> -U <POSTGRES_USER>
Tips:
- Use the
docker ps
command to find out the value of the<CONTAINER_NAME>
token.<POSTGRES_HOST>
,<POSTGRES_USER>
and<POSTGRES_PORT>
uses the same value stored in the .env file.
All the PostgreSQL data is stored under the psql
docker volume.
To run the application in development environment run:
$ npm run dev
To build the project for distribuition run:
$ npm run build
To run the final application run:
$ npm start
$ npm test
Notes:
- The src/index.ts file was excluded from testing because it is not a typescript module.
$ npm run lint
$ npm run coverage
The results will be stored at ./coverage folder
Notes:
- The src/index.ts file was excluded from coverage because it is not a typescript module.
To build the image container, use:
# docker build -t <IMAGE-NAME> .
After the image building, run the following command:
# docker run --rm -it -v"$(pwd)":/app --name <CONTAINER-NAME> <IMAGE-NAME>
Notes:
<IMAGE-NAME>
as the image name.<CONTAINER-NAME>
as the container name.
In place of build/run procedures, you may use just the docker-compose as follow:
# docker-compose up
# docker-compose start
# docker-compose stop
In order to remove the container, the associated images, orphans containers and volumes, use:
# docker-compose down --rmi "all" --remove-orphans --volumes