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
  • Overview
  • Location
  • Code
  • Analysis
  • Test
  • Summary of the Flow
  1. js_library
  2. Node Server
  3. 05 - Model View Controller
  4. 03 - Controllers

01 - Controller Set up

Previous00 - Controllers IntroNext02 - Create Method

Last updated 7 years ago

In this module, we'll get started on an endpoint that handles a simple POST request.

Overview

Here's the flow of what we will have after this module:

Notice that there is no database or models at this point.

Location

We're going to be adding to our testcontroller.js file that we built back in our lessons on routing.


        Node-Server
            └── controllers
              └── testcontroller.js
            └── models
            └── app.js
            └── db.js

Code

Before proceeding, please clear out all previous test methods in testcontroller.js. Add the following items to the file:

var express = require('express')
var router = express.Router()
var sequelize = require('../db');

/****************************************
 * Controller Method #1: Simple Response
****************************************/
      //1       //2           
router.post('/one', function(req, res){
  //3
  res.send("Test 1 went through!") 
});

module.exports = router;

Analysis

  1. We use the Express router object to call the post() method. This corresponds to the type of HTTP request that we are sending. POST is telling the server that the incoming request has data coming with it. You use a POST request when you sign up for an application, send an email, send a tweet, post on a wall, etc. POST sends data through HTTP to the server, which might send the data to the database to be stored.

  2. /one will be the endpoint/route we are using. Our route will be named http://localhost:3000/test/one. After that, we'll run a callback function, which will fire off a response.

  3. When the client requests the given endpoint, we simply send a string in our response.

KEY POINT: Notice that we are not yet talking to our model or database. We are simply sending an empty POST and returning a string response.

Test

Let's test this in Postman. 1. Make sure your server is running. 2. Open Postman. 3. Open a new request. 4. Change the dropdown to POST. 5. Enter the endpoint into the URL input field: http://localhost:3000/test/one 6. Press 'Send'. 7. You should see the following response:

Summary of the Flow

In this module, the following flow is happening:

1. We make a POST request with Postman.

2. The router sends that request to the testcontroller.

3. The testcontroller method fires off a callback with a response.

4. The callback sends back the response to Postman.

screenshot
screenshot