CRUD App - Create (POST)
Create a POST request handler that will take the data from the "Add Player" form and insert the new player into the database.
When a user fills out the "Add Player" form and clicks the submit button, it will send a POST request to /add (as specified in the attributes of the form). The data will be labeled based on the name attributes of the input elements. The server should take that POST request, and use the data to insert a new player into the database.
Hooking up an "Add Player" Request Handler
- In the routes/player.js file, add a new module export property named
addPlayer- Make sure to add a comma after the
addPlayerPagefunction
- Make sure to add a comma after the
- Set the value of
addPlayerto be a function withrequestandresponseas parameters - In the body of the
addPlayerfunction, logrequest.bodyto the console - After the console log, redirect to the homepage with
response.redirect('/') - In the app.js file, under the
app.set, tell theappto use the proper middleware to handle POST data:app.use(express.urlencoded({ extended: false })); app.use(express.json()); - In the app.js file, under the
app.getroutes, add anapp.postto hook up theaddPlayerfunction to the/addroute:app.post('/add', player.addPlayer);
Load up the "Add Player" page, submit the form, and verify that the form data is properly logged in the console!
Handling the POST Data
Now that the data is properly in the back-end, it's time to put it in the database. Update the db.js and routes/player.js files to make this happen.
DB Function
Start by creating a function that can add a new player object to the database.
- In the db.js file, add a new module export property named
addPlayer - Set the value of
addPlayerto be anasyncfunction withplayerObjas a parameter - In the body of the
addPlayerfunction, create a new variable namednewId - Set
newIdto the "player_" prefix, plus a new UUID- This will be the unique key for the database
- Under that, call
db.setto set the key (newId) to the value (playerObj) - Use the
awaitkeyword in front of thedb.setso it returns properly
Code
module.exports = {
addPlayer: async function(playerObj) {
let newId = "player_" + uuid.v4();
await db.set(newId, playerObj);
}
}
Route Function
Next, update the /add POST handler to use the DB function to insert the data into the database.
- At the top the routes/player.js file,
requirethe db.js module withrequire('../db')- Store the value in a
const db
- Store the value in a
- In the
addPlayerfunction, remove theconsole.log - In its place, add a call to
db.addPlayer, passing inrequest.bodyfor the player object - Put the
awaitkeyword in front ofdb.addPlayerso it returns properly - Add the
asynckeyword in front of the function because it uses anawait
Code
const db = require('../db.js');
module.exports = {
addPlayerPage: function (request, response) {
response.render('edit-player');
},
addPlayer: async function (request, response) {
await db.addPlayer(request.body);
response.redirect('/');
}
}
Testing
At this point, it should be fully possible to add Player data to the database.
- Run the app, and go to the
/addpage - Enter information about a new player
- Click the "Add Player" button
- Verify that the homepage loads
- In Repl, open the Database section on the left, and verify that there is some data in there!
