06 - Test Post
In this module, we'll write another client side function that access our server.
Overview
This time, we'll be testing our POST request from the test/one
endpoint that we already built, which will pull from the database instead of a pre-defined value. We'll chain promises together to create a flow chart for our fetch
.
Scripts
Let's add the function now. It should go directly below the first function in 01-scripts.js
:
Quick Summary
We are not seeing new stuff here, but let's do a quick summary: 1. We are fetching the url. The route in the server handles a POST
request, so our method type is POST
. Remember that these two must match. If a route takes a POST
request, then the declared method in the request should POST
. 2. We pass the response into a Promise that returns the response as plain text. (We'll use more JSON later) 3. We handle an error, if an error comes back. 4. In the final then()
, we simply print the plain text response to the console. This section is where we can do some DOM set up.
Test
Make sure that both your client and server are running.
Go to
localhost:8080
Click the
POST to /one
button.You should see the following success message:
Last updated