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:
lettestData="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){ //1vartestData=req.body.testdata.item;TestModel.create({//2testdata:testData})res.send("Test three went through!")console.log("Test three went through!")});module.exports=router;
Here we use the req.body middleware provided by Express and append two more properties to it. This is what we're sending to the database. req is the actual request, and body is where our data is being held. testdata is a property of body, while item is a property of testdata. 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-parser library and store it in the bodyParser variable.
This app.use statement MUST go above any routes. Any routes above this statement will not be able to use the bodyparser library, so they will break. You should read through this article to get a starter understanding of how body-parser is working with req.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 want json to be used as we process this request.
Enter the endpoint into the URL: http://localhost:3000/test/three.
Click on the body tab under the url input field.
Choose the raw radio 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 in req.body.testdata.item. This information is inside body, which is inside req.
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 Execute button (the lightning bolt).
screenshot
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.
var express = require('express');
var app = express();
var test = require('./controllers/testcontroller')
var sequelize = require('./db');
var bodyParser = require('body-parser'); //1
sequelize.sync(); // tip: {force: true} for resetting tables
app.use(bodyParser.json()); //2
app.use('/test', test)
app.listen(3000, function(){
console.log('App is listening on 3000.')
});