This project was originally a backend for a chat application, but it was never finished. The only functionality that is finished is authentication. Thus the name change to expressjs-authentication
The API endpoints are documented with swaggerUI.
The endpoint for the swagger docs is: /api-docs
As databse, this project uses mongodb, more on how to set that up later in this guide.
If you want to know more about the project structure, naming conventions... look at our wiki.
In this guide, you will be explained how to run this project on your own device.
- Dev environment
- Setup scripts
- Setup mongodb
- .env file
- Run the project
- Run the project (with docker)
- Running tests
It is important that everyone uses the same environment for developing the project. It is also much easiers, and saves a lot of time.
This is easily achievable with the vscode remote-containers, dev-containers that can be opened in vscode. Take a look at this extension. When the extension is installed, you should have an option to open the project in a new remote-container.
If you want to develop using our development environment, make sure to have docker installed, as this is needed.
In the /bin
directory, you can find some usefull scripts that make it even easier to work with docker.
- startMongo.sh : start a new mongoDB container (with authentication USERNAME='admin', PASSWORD='admin')
- stopMongo.sh : stop running mongoDB container + delete the container
- startAll.sh : run docker-compose, and rebuild all the images for when changes are made
- stopAll.sh : stop docker-compose, and remove all the containers and docker network
In this part of the guide, the setup for mongodb is explained.
Ofcourse, you can use any tutorial you want to setup a mongodb installation.
It is recommended that you install mongodb in a docker container, as it's much easier, and makes the install non-permanent. It's very easy to delete the docker container when you don't want to use mongodb anymore.
Setup mongodb without authentication (a root user account):
sudo docker run --name mongodb -d -p 27017:27017 mongo
Setup mongodb with a root user account (of course you can change the uesrname and password to your liking):
sudo docker run \
--name mongodb \
-d -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=admin \
mongo
Mongosh is a new addition for mongodb, it's an interactive shell to interact with your database.
This can also be used to test your database connection, and to test if the authentication was succesfull.
To test the connection, run following command:
mongosh
To test a connection, and also login as the created root user:
mongosh -u [username] -p [password]
To start, stop or remove the docker container, use following commands:
sudo docker start [containerName]
sudo docker stop [containerName]
sudo docker rm [containerName]
Thourghout the application, environment variables are often used. Below, and expample of how a .env file looks, this file should be placed in the root of the project:
PORT=3000
MONGODB_URI="mongodb://localhost:27017"
MONGODB_DATABASE="chat-app"
MONGODB_USERNAME="admin"
MONGODB_PASSWORD="admin"
COOKIE_SECRET="SECRET"
JWT_SECRET="SECRET"
Of course, change the MONGODB variables for your own environment.
Secure secret strings for the cookie and the jwt can be generated by running following command in the terminal:
openssl rand -base64 64
This will generate a random string of 64 base64 characters
After cloning the repo, and setting up the database, it's very easy to run the application.
install the node modules: npm install
run the application: npm start
or node index.js
Make use of hot-reloading: npm run dev
or nodemon
(make sure you have nodemon installed)
If you prefer, the project can also be ran by using docker.
The Dockerfile builds the image for this project.
This image, and the database (monogodb) are ran in seperate containers which can be ran with docker-compose.
Start the container-cluster with:
docker-compose up -d
Use the -d flag to start the cluster up in the background
Docker-compose can be stopped with:
docker-compose stop
If you are contributing to this project, you don't want to manualy test to see if everything still works, instead just run the tests this project has.
To run the tests, use following command:
npm test
For testing, we use the framework mocha, nyc is used to give test coverage when you end the tests.