?> Author: Jimmy Lan, Date Created: 2021-03-12, Last Updated: 2021-04-11
In this article, we will address three ways to run a local copy of this project. We will explore:
After cloning the project, run
npm installMake a copy of the file .env.example.
Name it exactly as .env.
Look in to the file and change the variables to your liking.
In particular, please provide a connection string to your Mongo DB, and a connection string to Redis.
If you choose to run the databases locally on your machine for the purpose of development, simply paste in the value mongodb://localhost:27017 for Mongo DB and redis://localhost:6379 for Redis.
To give you some context, we use Mongo DB to store the main data values for this project. We use Redis to store temporary values, for example, data related to our rate limiting implementation.
If you run Mongo DB or Redis, or both, on your local machine, please make sure to start the services before going to the next step.
?> Usually, we run the command mongod to start a local copy of Mongo DB, and redis-server to start Redis.
Run
npm run devto start a development server which auto-reloads whenever the code changes.
Alternatively, you may do this manually. If you run
npm run buildthen, the Typescript code will be compiled to Javascript and stored in the dist folder.
You may then run
npm startto start the server using the code in the dist directory.
Note that the npm start method do not rebuild the project on code change.
In this section, we will run the web server alone in a container using Docker.
At the current stage, I have not added separate docker files for Redis and Mongo DB.
It is currently unclear whether I should add this feature to the project.
Therefore, we are not able to use docker-compose just yet.
Updated April 11, 2021:
A docker-compose.yml file can be found at the root of the project repository.
It contains instructions to set up the web server, nginx, mongo db, and redis.
You may choose to uncomment the sections for nginx and mongo db based on your needs.
Please refer to Run Project with Docker-compose for more information.
Run the following command from the root of this repository:
docker build -t lanyanxi/node-auth . -f dev.DockerfileTo start the server, use:
docker run -p 5000:5000 -it lanyanxi/node-author you may run the container in detach mode:
docker run -dp 5000:5000 -it lanyanxi/node-authBe sure to pass in the environment variables and change the port if you decide to use a different one.
You can find a file named docker-compose.yml from the root folder of this project.
Edit the file to suite your needs, then run:
docker-compose upIf you are using a container-optimized OS (for example, a container in the cos-cloud project), you may not be able to install docker-compose directly.
A work-around is to run docker-compose from Docker.
For example, use the following command to download and run a docker-compose image:
docker run docker/compose:1.24.0 versionWe can then run the project using the following command as an alternative to docker-compose up:
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PWD:$PWD" \
-w="$PWD" \
docker/compose:1.24.0 up?> Tips: Please consult the documentation written by a Google employee for more information: https://cloud.google.com/community/tutorials/docker-compose-on-container-optimized-os.
To make future dev-ops work easier, we recommend you to register an alias for the long command above, as follows:
echo alias docker-compose="'"'docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PWD:$PWD" \
-w="$PWD" \
docker/compose:1.24.0'"'" >> ~/.bashrc && source ~/.bashrcNow you can run docker-compose up normally.