JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • The Model-View-Controller Software Pattern
  • Views
  • Controllers
  • Models
  • Endpoints & Routes
  1. js_library
  2. Node Server
  3. 05 - Model View Controller
  4. 01 - MVC

00 - MVC Intro

Previous01 - MVCNext02 - Models

Last updated 7 years ago

In this chapter, we'll walk through several modules that will teach how to build controller methods and routes that make POST requests containing data. Those requests will post data that will persist in our Postgres database and return that data to the client in a response. To write this code, we'll use a Model-View-Controller pattern, so we'll introduce you to MVC at this time.

The Model-View-Controller Software Pattern

MVC is a common software pattern found in many programming languages and applications. Take a look at a diagram of MVC to start getting an idea of how it works:

Views

For this chapter, we won't be dealing with the view, but we'll say that the view/client would be sending requests, receiving responses, and working to display the data to users. For a temporary client, we'll use Postman in lieu of a View.

Controllers

It's good to think of the controller as something that handles the heavier logic in the application. A controller is usually a method or methods that will handle some or all of the following things:

  1. Receiving the incoming request depending on the route.

  2. Processing the type of incoming request: GET, POST, PUT, DELETE.

  3. Collecting the data from the incoming request.

  4. Working with the model to ensure that the request data matches the types in the model and the database.

  5. Creating, updating, reading, or deleting objects in the database.

  6. Sending off the response for the incoming request.

Models

Coding models are usually considered to be just this: representations of the data being handled. Models can do the following:

1. Represent the data being stored in the database.

2. Dictate the types of data that will be stored (string, boolean, integer).

3. Handle some basic business logic in an application, such as a character limit for a string being stored in the db, formatting for date and time details, and many other things that shape the data in the database.

4. Used by the controller to handle logic.

Endpoints & Routes

As mentioned in a previous module, when an HTTP request comes in, it hits a route and finds the proper endpoint. When the router finds the proper endpoint, the proper controller method is fired, and the controller method handles any necessary logic. To practice learning about endpoints, controllers, and models, we'll be building the following endpoints in the modules ahead:

http://localhost:3000/test/one - POST 
http://localhost:3000/test/two - POST
http://localhost:3000/test/three - POST
http://localhost:3000/test/four - POST
http://localhost:3000/test/five - POST
http://localhost:3000/test/six - POST
http://localhost:3000/test/seven - POST

When we start up our server, each of these will be available for processing a request. We'll work with each one as a way to learn the process of writing a controller method.

Before we fire off controller methods, we'll start by building some data models.

screenshot