The goal of this web application is to get practice with developing MERNG tech and to use the finished application as product to test against. There will be 4 branches in this repo as follows:
main
- the base applicationjest
- contains jest-based unit testsrtl
- contains React Test Library built testscypress
- contains Cypress end-to-end tests
- MongoDB
- Mongoose ORM
- Node.js
- Apollo Server
- GraphQL
- React
- Semantic UI
- Docker / Docker Compose
- Clone project & install dependencies:
$ git clone [email protected]:tim-corley/mern-social-app.git
$ cd mern-social-app/server && yarn install
$ cd mern-social-app/client && yarn install
- Create an
.env
file at project root & add the following variables in order to initialize the database:
MONGO_INITDB_ROOT_USERNAME=<my_user>
MONGO_INITDB_ROOT_PASSWORD=<my_pwd>
MONGO_INITDB_DATABASE=<db_name>
- Within the
server/
directory, create aconfig.js
file and add the following in order to initialize the server:
const DB_HOST = '<mongo-container-name>';
const DB_PORT = 27017;
const DB_USER = '<my_user>';
const DB_PASS = '<my_pwd>';
const DB_NAME = '<db_name>';
const JWT_SECRET = '<random-key>';
module.exports = {
MONGODB: `mongodb://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`,
JWT_SECRET,
SERVER_PORT: 5000,
};
Note that is it important here to have DB_HOST
exactly match the mongo service containter name (set in docker-compose.yml
). Also, be sure that DB_PORT
, DB_USER
, DB_PASS
, & DB_NAME
match the values used for the docker-compose.yml
mongo service config (specifically, ports & environment)
- Start Containers (DB, Server, Client):
$ docker-compose up --build
Docker Compose is using volumes for all containers in order to enable hot-reloading & data persistence.
- Stop & Remove Containers:
$ docker-compose down
-
Explore the database with Mongo Shell
A) Get container id:
$ docker ps
B) Access (running) container's shell:
$ docker exec -it <container_id> /bin/bash
C) Start Mongo Shell (pass in username used in docker-compose file):
root@84f9e1a69db9:/# mongo --port 27017 --authenticationDatabase "admin" -u "<root_user>" -p
D) Interact with database:
> show dbs admin 0.000GB config 0.000GB local 0.000GB social-app 0.000GB > use social-app switched to db social-app > show collections posts users > db.users.find().pretty() ...
-
Start Mongod instance:
-
$ sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb
-
Connect with Mongo Shell:
-
$ mongo --port 27017 --authenticationDatabase "admin" -u "adminAccount" -p
-
Create a new database for the project and add an admin user:
> use social-app switched to db social-app > db.createUser( ... { ... user: "newuser", ... pwd: "awesomepwd", ... roles: [ "readWrite", "dbAdmin" ] ... } ... ) Successfully added user: { "user" : "newuser", "roles" : [ "readWrite", "dbAdmin" ] }
-
Start the development server:
$ nodemon index.js