03 - req.body()
In this module, we'll set up our Express application so that it can take incoming requests properly.
Overview
Hopefully, your model works, and you were able to put something in the database. That's great, but if we continue doing a POST request, we would send the same string every time. This one:
let testData = "Test data for endpoint two";We definitely want to be more versatile with incoming requests. To do that, we need another tool called body-parser, and we want to use the req.body middleware tool provided by Express. This is confusing, so let's step through it slowly.
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:
/****************************************
* Controller Method #3: req.body
****************************************/
router.post('/three', function (req, res) {
//1
var testData = req.body.testdata.item;
TestModel
.create({ //2
testdata: testData
})
res.send("Test three went through!")
console.log("Test three went through!")
});
module.exports = router;Here we use the
req.bodymiddleware provided by Express and append two more properties to it. This is what we're sending to the database.reqis the actual request, andbodyis where our data is being held.testdatais a property ofbody, whileitemis a property oftestdata. We'll see this in Postman in a little while.create()is a Sequelize method. It creates a SQL statement that will insert our data into the database. You'll learn more about SQL later.
body-parser
In order to use the req.body middleware, we need to use a middleware package called body-parser. We installed this earlier. Go into app.js and add the following code:
Analysis
We pull in the
body-parserlibrary and store it in thebodyParservariable.This
app.usestatement MUST go above any routes. Any routes above this statement will not be able to use thebodyparserlibrary, so they will break. You should read through this article to get a starter understanding of howbody-parseris working withreq.body. Warning: this will lead you down a rabbit hole of understanding. For our purposes, it's important to know this:
app.use(bodyParser.json())tells the application that we wantjsonto be used as we process this request.
Here is a little more information on body-parser.
Testing
Make sure your server is running.
Open Postman.
Open a new request.
Change the dropdown to POST.
Enter the endpoint into the URL:
http://localhost:3000/test/three.Click on the body tab under the url input field.
Choose the
rawradio button.In the dropdown, choose
JSON (application/json).In the empty space, add a JSON object like the one below. Notice that this is
testdata.item, the last two properties inreq.body.testdata.item. This information is insidebody, which is insidereq.Press send.
You should see the following
Let's also go to Postgres and make sure the data is there. To update the table, you can press the
Executebutton (the lightning bolt).
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 /three 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, and Postgres stores it.
8. The controller sends a response to Postman.
Last updated