Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active April 26, 2021 19:00
Show Gist options
  • Save ross-u/2dd655523226b09792385b3f19a343dc to your computer and use it in GitHub Desktop.
Save ross-u/2dd655523226b09792385b3f19a343dc to your computer and use it in GitHub Desktop.
M2 - project setup (ExpressJS + Mongoose + Handlebars)

M2 - project setup (ExpressJS + Mongoose + Handlebars)


README

The below steps should be done only once your project idea was verified by your captain and you have your documentation/README completed.


Create the project locally

Create the project locally using express-generator. Run the express-generator command below and specify the name of the project:

npx express-generator --view=hbs your-project-name


Setup the project


Add the .gitignore file


Create the .gitignore file in the root folder of the project.


touch .gitignore

Add the file/folder exclusion rules to the .gitignore file. You can find and copy the file content containing these rules from the .gitignore boilerplate for NodeJS projects on the GitHub's repo here.


❗ ❗ IMPORTANT: Double check that you have the .gitignore file, and that it contains a rule for the .env file which prevents this file from being uploaded to the repository and being visible publicly.

This is very important. Please keep this in mind during your future projects which may contain API keys, session secrets, etc.


Create .env file


Create the .env file in the root folder of the project.


touch .env

In the .env file add the following variables, but remember to update the MONGODB_URI part saying your-db-name, where you should put the name of your database :


.env
PORT=3000
MONGODB_URI=mongodb://localhost:27017/your-db-name
SESSION_SECRET=ADD-A-RANDOM-STRING


Setup git and GitHub


Include your README file in the project


Add the README.md file with the finished project documentation to the root directory of the project.


Initialize git and create repository on Github


  1. From the root folder of the project run the following commands to initialize git.
git init

git add .

git commit -m 'Initial commit'

  1. Open GitHub and create a new repository for your project.

  2. Copy the git remote add origin ... command from the GitHub setup instructions and run it locally from the terminal.


Example:


  1. After you are done with the above step, push the initial commit and the master branch to GitHub:


    git push origin master

Your local repository and your GitHub repository are now successfully connected!


Create the develop branch


Next step is to create the develop branch locally and push it's copy to GitHub.

Create an additional develop branch (Your project will have 2 main branches master and develop).

While in the root folder of the project, run the following commands:


# Create a new branch - `develop`
git checkout -b develop

# Push the new branch to the GitHub repository
git push origin develop

Add the repository Collaborator/s


The team member who is the owner of the repository should add the other team member as the Collaborator.

To do this, while on the page of the projects GitHub repository:

  1. Open the Settings tab.
  2. In the left-hand side menu select Manage access.
  3. Click on the button Invite a collaborator.
  4. Search for and add the other team member by the GitHub username or email.
  5. The other team member must accept the invitation (either by email that is automatically sent, or by the invitation link).


Next step - Other team member/s


After the project/repository owner does the above steps and pushes the new develop branch and the updates to GitHub, the other team member/s should clone the repository and pull all the branches.


In the below example the angle brackets < > indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.

git clone <url-of-the-project-repo>

Once cloned the team member/s should navigate to the root directory of the cloned project and run the following command to pull the develop branch, will download the develop branch and make it available locally for the other teammember :


git pull origin develop


Git Flow Main Guidelines


  • master branch will be used only for deployment.

  • develop branch will be used for working/development. When working on a feature create a new branch from develop.

    Tips to avoid merge conflicts:

    • Work on separate files, and communicate with your partner.
    • Initial setup of the back-end should be preferably done in pair, taking turns while coding and preferably coding on one laptop (pair programming).
    • Do daily checkpoints with the partner and communicate things that you will be doing and parts of code that you will be working on.
    • Make smaller commits, and write descriptive messages:
      • Max. 50 characters (over 50 will wrap in the message body) )
      • Use imperative e.g. :
        • "Add session middleware to app.js "
        • "Add UserModel.js"
        • "Bugfix route '/login' in routes/auth.js "


Working with branches:


In the below example the angle brackets < > indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.

# Create a new branch and move to that branch
git checkout -b <branch-name>



# Move to an existing branch
git checkout <branch-name>



# Check the branch you are on and
# List all the existing branches
git branch



# Delete a branch 
git branch -D <name-of-the-branch-to-delete>



# Merge code from another branch into a current branch
git merge <branch-from-which-to-merge>


# List the created commits on the current branch
git log



Additional Resources


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment