Skip to content

mstrivens/not_my_cat_backend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Not My Cat ✧/ᐠ-ꞈ-ᐟ\

Not My Cat is a responsive single-paged web app providing a platform for cat lovers to track, collect and play with cats in the real world. Our users can create a cat, with name, picture, attributes and their location stored using the Google Maps API. They can then play against randomised cats, to increase their win total and win crowns. Finally they can see where other cats have been spotted and go out to spot these neighbourhood felines.

We created Not My Cat as part of our final project at Makers Academy, a 12 week web development bootcamp. We used the MERN stack (Mongo DB, Express, React, Node.js)

implementation of the Express.js server

  • npm init

  • npm install dependancies

    • dotenv for accessing enviroment data needed to connect to Mongodb
    • express for the router functions
    • mongoose for connecting and carrying out actions on Mongodb
    • supertest for testing requests to the router
    • jest as a testing framework, coverage and more
    • nodemon to rolling restart the server on saved changes
    • eslint as a linter
  • updated scripts in the package.json

"scripts": {
    "dev": "nodemon app.js",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage --colors"
  }
  • added dirs:

    • models
    • config
    • routes/api
    • test/helper
  • touch:

    • ./app.js
    • ./test/app.test.js
    • ./test/users.test.js
    • ./config/db.js
    • ./.env
    • ./routes/api/users.js
    • ./models/user.js
  • added jest configuration to package.json

"jest": {
    "testEnvironment": "node",
    "testTimeout": 10000
  }

app.js

  • app.js is updated to pull in express.js and a few of the other dependancies and the empty routes files, a test is written for a basic route and then the route added to pass.
  • A port is assigned to the server.
  • Middleware is added to the app.js to ensure that json can be parsed for post requests. (This has to be declared before any of the routes otherwise it will lead to errors where no body is passed to the post routes.)
  • The db connection method is delcared for dev and production envs.

users.test.js

  • Requires app, testSetup and supertest to be pulled in.
  • The tests are in a familiar format in jest to jasmine and rspec et al.
  • The request const, instantisation of supertest allows you to make requests to the server from the test suite.
  • Tests involving requests must be async or use the done callback.

model/user.js

  • A mongoose schema object that defines the user model in our case. the mongoose documentation helps with how we can define our attributes.
  • The function userSchema.pre("save") allows us to do things to data before it is saved to the database, in this instance we are gonna encrypt the password with Bcrypt.
  • The function userSchema.methods.isValidPassword() is used to check the password for when the user wants to login or authenticate. It returns a boolean.

config/db.js

  • Using mongoose to create functions to connect to and disconnect from the database.
  • We use the env variable MONGO_URI which we access using dotenv to connect to the database and make sure that our credentials are not pushed to github.

test/helper/dbHelper.js

  • This helper file allows the tests to connect to the database use a beforeAll and afterAll function.
  • Also there are beforeEach and afterEach functions that create stand up test data and tear it down so that each of the tests can be carried out in a clean environment.
  • This file calls on testData.json that contains the data for the test database seeding.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 97.5%
  • Shell 2.5%