Coder Social home page Coder Social logo

personal-site's Introduction

Share-a-note

Share-a-note is a wiki-style app for organising notes using a MERN stack.

Users can create an account, log-in and start creating notes. Notes can be tagged for easy organisation. Notes can include images. Notes can be deleted and edited. Notes can be searched.

The main view is split into three easy to navigate sections: tag list, note list and note editing area.

Trello Link

https://trello.com/b/HGz4vQoe/share-a-note

Miro Board

https://miro.com/app/board/uXjVPRMAEws=/

Pre-existing Features

It was already possible for a user to:

  • Sign up
  • Sign in
  • Sign out
  • View a list of notes

Technologies

This project used a 'MERN' stack

M is for MongoDB

MongoDB is a NoSQL database program that stores data in collections of JSON-like structures, rather than in tables. The application interracts with MongoDB using a tool called Mongoose.

E is for Express

Express is the Javascript equivalent of Sinatra. The structure of this application will feel quite different to what you're used to but the principles are the same.

R is for React

React is a hugely popular tool that is used to build engaging front ends. The basic principle is that the front end is split up into components, each of which could include some logic, template structure (HTML) and styling (CSS).

N is for Node

Java script was originally designed to run exclusively in browsers, such as Chrome. Node is a tool that allows you to run Javascript outside the browser and its invention made it possible to build full stack Javascript apps.

We also used...

  • Jest for unit testing on the back end
  • Cypress for end-to-end testing and component testing, on the front end
  • Mongoose to model objects in MongoDB.
  • ESLint for linting.
  • Nodemon to reload the server automatically.
  • JWT for security.
  • Heroku for backend and frontend hosting.
  • MongoDB-Atlas for database hosting.
  • Bootstrap for frontend styling.

Architecture

This application is comprised of two distinct pieces.

  • An backend API built with Express
  • A front end built with React

The React front end sends HTTP requests to the backend API and receives JSON in response body, rather than a whole page of HTML.

For example, the React front end would send this request to retrieve the entire Notes collection.

GET "/notes"

And the body of the response would look like this.

{
    "notes": [
        {
            "_id": "62f8ef0e6c1ffcf74cbbb181",
            "title": "My first note",
            "noteContent": "This is a new note"
            "__v": 0
        },
        {
            "_id": "62f8ef0e6c1ffcf74cbbb181",
            "title": "My second note",
            "noteContent": "This is my second note"
            "__v": 0
        },
        {
            "_id": "62f8ef0e6c1ffcf74cbbb181",
            "title": "My third note",
            "noteContent": "This is my third note"
            "__v": 0
        }
    ]
}

Here's a diagram of the above

a diagram of the mern stack

Once recieved by the React FE, the JSON in the response body is used to render a list of notes on the page.

reponse body mapped onto a page

This architectural pattern is quite popular because it allows teams to build multiple front ends, all of which use the same backend API. You could, for example, go on to build a mobile app without needing to create another backend API.

Authentication

Up until now, if you've implemented authentication, it will likely have been done using sessions - this is a useful point of comparison but, if you haven't implemented authentication yet, that's not going to impede you right now.

Here's the authentication flow for this application

  1. A registered user submits their email address and password via the React front end.
  2. The Express backend receives the data and tries to find a user in the DB with the same email address.
  3. If a user is found, the password in the database is compared to the password that was submitted.
  4. If the passwords match, a JSON Web Token is generated and returned, as part of the response.
  5. The React front end receives the token and holds on to it.
  6. Every request to "/notes" must include a valid token (which is checked by the backend).
  7. When the user logs out, the front end discards the token.

authentication flow diagram

What is a JSON Web Token?

A JSON Web Token, or JWT, is a token that comprises three parts

  • A header, which contains information about how the token was generated.
  • A signature, which is used to verify the token.
  • A payload, which you can use to store some non-sensitive data like a user id. Note that the payload is not secure and can be decoded very easily.

The signature is created using a 'secret', which must be kept private (i.e. not put on GitHub) otherwise nefarious internet users could start to issue tokens for your application.

Here, we've used an environment variable called JWT_SECRET, which you'll see used in the commands to start the application and run the tests (below). You can change the value of that environment variable to anything you like.

Quickstart

If running the project locally, do the following:

  1. Install Node Version Manager (NVM)
    brew install nvm
    
    Then follow the instructions to update your ~/.bash_profile.
  2. Open a new terminal
  3. Install the latest version of Node.js, currently 18.1.0.
    nvm install 18
    

Cloning and setting up the project to run locally:

  1. Clone our repo to your local machine
  2. Install Node.js dependencies for both FE and BE (API)
    ; cd api
    ; npm install
    ; cd ../frontend
    ; npm install
    
  3. Install an ESLint plugin for your editor. For example: linter-eslint for Atom.
  4. Install MongoDB
    brew tap mongodb/brew
    brew install [email protected]
    
    Note: If you see a message that says If you need to have [email protected] first in your PATH, run:, follow the instruction. Restart your terminal after this.
  5. Start MongoDB
    brew services start [email protected]
    

Start

  1. Start the server

Note the use of an environment variable for the JWT secret

; cd api
; JWT_SECRET=SUPER_SECRET npm start
  1. Start the front end

In a new terminal session...

; cd frontend
; npm start

You should now be able to open your browser and go to http://localhost:3000/ to view our welcome page. You should sign in to view the main feed and create posts.

Testing

The Backend (API)

Note the use of an environment variable for the JWT secret

Start the server

; cd api
; JWT_SECRET=SUPER_SECRET npm start

Then run the tests in a new terminal session

; cd api
; JWT_SECRET=SUPER_SECRET npm run test

The frontend (React)

Note the use of an environment variable for the JWT secret

Start the server

; cd api
; JWT_SECRET=SUPER_SECRET npm start

Then start the front end in a new terminal session

; cd frontend
; JWT_SECRET=SUPER_SECRET npm start

Then run the tests in a new terminal session

; cd frontend
; JWT_SECRET=SUPER_SECRET npm run test

MongoDB Connection Errors?

Some people occasionally experience MongoDB connection errors when running the tests or trying to use the application. Here are some tips which might help resolve such issues.

  • Check that MongoDB is installed using mongo --version
  • Check that it's running using brew services list

If you have issues that are not resolved by these tips, please reach out to a coach and, once the issue is resolved, we can add a new tip!

personal-site's People

Contributors

nascho avatar tigranargit avatar edwardandress avatar nickwlong avatar v-saigal avatar esrak1 avatar razvanbugoi avatar maricel30 avatar johnforster avatar kuwotu avatar jamesjoshuahill avatar penguat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.