Skip to content

Christopher-Allen-21/psu-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PSU Masters Sample Backend/API

  • Objective - Implementat a sample API using Express, MongoDb, and Swagger

To Run

  • Once respository is cloned locally, user can run either of these commands:
    • node index.js - runs server locally; will need to stop, save, and re-run when modifying
    • npm start - runs server locally; automatically re-runs after saving

NPM packages to install

  • npm init -y - setup new project and creates package.json
  • npm install express - installs express
  • npm install mongodb - installs mongodb
  • npm i mongoose - installs mongoose
  • npm i dotenv - installs dotenv (used for environment variables)
  • npm install --save-dev nodemon - installs nodemon
  • npm install yamljs - installs yaml
  • npm install swagger-ui-express - installs swagger UI
  • npm install axios - installs axios (used to make request to 3rd party API)

Additional Resources

Code Walkthrough

  • index.js
    • image
      • The required libraries needed for Express, MongoDb, and Swagger
      • Initializing express and using .env file for setting the port and mongodb url
      • Setting up swagger for "/api-docs" endpoint
      • Connecting to mongodb and using the PORT from env
    • image
      • GET endpoints
      • "/" returns a welcome message
      • "/users" returns all users with 200 response or 500 response if there is server error
      • "/users/:id/" returns one user based on id with 200 response, 404 if user cannot be found, or 500 if there is a server error
    • image
      • POST endpoint
      • "/users" creates a new user with 201 response, 400 if the body fails validation, or 500 if there is server error
    • image
      • PUT endpoint
      • "/users/:id" updates an existing user with 200 response, 404 if user cannot be found, 400 if the body fails validation, or 500 if there is a server error
    • image
      • DELETE endpoint
      • "/users/:id" deletes an existing user with 200 response, 404 if user cannot be found, or 500 if there is a server error
  • user.js
    • image
      • Defines the User schema
      • Makes certain fields required
  • swagger.yaml
    • image
      • Defines how Swagger UI will appear

Functionality Demo

  • GET Request
    • image
    • image
    • image
  • POST Request
    • image
    • image
    • image
  • PUT Request
    • image
    • image
    • image
  • DELETE
    • image
    • image
    • image
  • Swagger UI
    • image
    • image
    • image
    • image
    • image
    • image
  • MongoDB
    • image

Consuming 3rd Party API

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published