Part 3: Adding Routes / Middleware to Support Adding Students Step 1: Adding the built-in express.urlencoded middleware
Question:
Part 3: Adding Routes / Middleware to Support Adding Students
Step 1: Adding the built-in "express.urlencoded" middleware For handling regular (text) form data and access the data on req.body, add the express.urlencoded({ extended: true }) middleware (using app.use()). Step 2: Adding "Post" route Add the following route: o POST /students/add 1. This route makes a call to the (promise-driven) addStudent(studentData) function from your data-service.js module (function to be defined below). It will provide req.body as the parameter, ie "data.addStudent(req.body)". 2. When the addStudent function resolves successfully, redirect to the "/students" route. Here we can verify that the new student was added Step 3: Adding "addStudent" function within data-service.js Create the function "addStudent(studentData)" within data-service.js according to the following specification: (HINT: do not forget to add it to module.exports) o Like all functions within data-service.js, this function must return a Promise o If studentData.isInternationalStudent is undefined, explicitly set it to false, otherwise set it to true (this gets around the issue of the checkbox not sending "false" if it's unchecked) o To set the studentID property of studentData, you need to do the followings: 1. In data/students.json or the "global" students array, the data type of the studentID property is string. So, try to get the number values corresponding to the studentID string values in the students array, then find the maximum number value, for example 999827145. 2. The maximum number value plus one (1) will be used for the new studentID of studentData, and don't forget to convert it to string type. This will have the effect of setting the first new student number to "999827146", and so on. o Push the updated studentData object onto the "students" array and resolve the promise. Step 4: Verify your Solution At this point, you should now be able to add new students using the "/students/add" route and see the full student listing on the "/students" route.