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

05 - Sending the Response

In this module, we'll pass the request data back in our callback function

Overview

We now have a proper sequence, but the way we displayed the success message might not be what we want. We actually are probably going to want the data to be sent back to us after it's been saved in the db. The current response res.send("Step four"); is in no way really connected to the actual data. What if, for instance, we were saving an item to a To-Do list? We would want the data to save, and then we would want to get it in our response. Let's pass some of the data into the callback to provide a more detailed response to the user.

The Code

Go into the testcontroller.js file and add the following method. Add it to the bottom of the file above the export statement.

/*********************
 * Route 5: Return data in a Promise
 **********************/
router.post('/five', function (req, res) {
  var testData = req.body.testdata.item;
  TestModel
    .create({
      testdata: testData
    })
    .then(              //1
      function message(data) {
        res.send(data);  //2
      }
    );
});
  1. It's important to note that the .then() method is chained to .create(). In fact, the whole expression could be read like this in one line:

    TestModel.create({testdata: testData}).then(function message(data) { res.send(data);});
  2. With that idea in mind, we have changed the data coming back in the response to the data that was persisted in Postgres. To be clear, after the data is persisted in the Postgres with the .create() method and in the testdata column, the .then() method returns a Promise that fires up a callback function holding the data that was just added.

It's important to note that the data parameter can have any name that we want.

Testing

Let's use Postman to test this:

  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: http://localhost:3000/test/five.

  6. Click on the body tab under the url input field.

  7. Choose the raw radio button.

  8. In the dropdown, choose JSON (application/json).

  9. In the empty space, add a JSON object like the one below:

{
    "testdata":{
        "item":"step 5"
    }
}
  1. Press send.

  2. You should see the following:

  1. Notice that the data in the response matches the data in the request.

  2. You should also go to Postgres and make sure the data is there and that the testdata column matches the request and response:

Summary of the Flow

In this module, the following flow is happening:

  1. We make a POST request with Postman.

  2. body-parser breaks the request into JSON.

  3. The router sends the request to the testcontroller.

  4. The controller with the /five endpoint is called.

  5. The req.body.testdata.item is captured in the testData variable.

  6. We then use the Sequelize create() method to create the object to be sent to the DB.

  7. The object is sent to Postgres, which stores it.

  8. After the data is stored, we fire the then() method, which returns a Promise.

  9. We call a method that takes in a parameter called testdata. It holds the response data.

  10. The method fires a response to Postman.

Previous04 - Crafting the ResponseNext06 - JSON Response

Last updated 7 years ago

screenshot
screenshot