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
addPlayerPage
function
- Make sure to add a comma after the
- Set the value of
addPlayer
to be a function withrequest
andresponse
as parameters - In the body of the
addPlayer
function, logrequest.body
to the console - After the console log, redirect to the homepage with
response.redirect('/')
- In the app.js file, under the
app.set
, tell theapp
to 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.get
routes, add anapp.post
to hook up theaddPlayer
function to the/add
route: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
addPlayer
to be anasync
function withplayerObj
as a parameter - In the body of the
addPlayer
function, create a new variable namednewId
- Set
newId
to the "player_" prefix, plus a new UUID- This will be the unique key for the database
- Under that, call
db.set
to set the key (newId
) to the value (playerObj
) - Use the
await
keyword in front of thedb.set
so 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,
require
the db.js module withrequire('../db')
- Store the value in a
const db
- Store the value in a
- In the
addPlayer
function, remove theconsole.log
- In its place, add a call to
db.addPlayer
, passing inrequest.body
for the player object - Put the
await
keyword in front ofdb.addPlayer
so it returns properly - Add the
async
keyword 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
/add
page - 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!