calm-atoll-83756.herokuapp.com/images/add ) to reconstruct the "Add Image" form (HINT: You can
…
div> element)
Step 5: Adding a home for the uploaded Images
• Create a new folder in your "public" folder called "images"
• Within the newly created "images" folder, create an "uploaded" folde
Part 2: Adding Routes / Middleware to Support Image Uploads
Step 1: Adding multer
• Use npm to install the "multer" module
• Inside your server.js file "require" the "multer" module as "multer"
• Define a "storage" variable using "multer.diskStorage" with the following options (HINT: see "Step 5: (server)
Setup…" in the week 5 course notes for additional information)
https:
calm-atoll-83756.herokuapp.com/employees/add
https:
calm-atoll-83756.herokuapp.com/images/add
https:
web322.ca/notes/week05
o destination "./public/images/uploaded"
o filename function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
• Define an "upload" variable as multer({ storage: storage });
Step 2: Adding the "Post" route
• Add the following route:
o POST /images/add
â–ª This route uses the middleware: upload.single("imageFile")
â–ª When accessed, this route will redirect to "/images" (defined below)
Step 3: Adding "Get" route / using the "fs" module
• Before we can add the below route, we must include the "fs" module in our server.js file (previously only in our
data-service.js module)
• Next, Add the following route:
o GET /images
â–ª This route will return a JSON formatted string (res.json()) consisting of a single "images"
property, which contains the contents of the "./public/images/uploaded" directory as an a
ay,
ie { "images": [" XXXXXXXXXXjpg", " XXXXXXXXXXjpg"] }. HINT: You can make use of the
fs.readdir method, as outlined in this example from code-maven.com
Step 4: Verify your Solution
At this point, you should now be able to upload images using the "/images/add" route and see the full file
listing on the "/images" route in the format: { "images": [" XXXXXXXXXXjpg", " XXXXXXXXXXjpg"] } .
Part 3: Adding Routes / Middleware to Support Adding Employees
Step 1: Adding body-parser
• Use npm to install the "body-parser" module
• Inside your server.js file "require" the "body-parser" module as "bodyParser"
• Add the bodyParser.urlencoded({ extended: true }) middleware (using app.use())
Step 2: Adding "Post" route
• Add the following route:
o POST /employees/add
â–ª This route makes a call to the (promise-driven) addEmployee(employeeData) function from your
data-service.js module (function to be defined below). It will provide req.body as the
parameter, ie "data.addEmployee(req.body)".
https:
code-maven.com/list-content-of-directory-with-nodejs
â–ª When the addEmployee function resolves successfully, redirect to the "/employees" route.
Here we can verify that the new employee was added
Step 3: Adding "addEmployee" function within data-service.js
• Create the function "addEmployee(employeeData)" 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 employeeData.isManager 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 Explicitly set the employeeNum property of employeeData to be the length of the "employees" a
ay
plus one (1). This will have the effect of setting the first new employee number to 281, and so on.
o Push the updated employeeData object onto the "employees" a
ay and resolve the promise.
Step 4: Verify your Solution
At this point, you should now be able to add new employees using the "/employees/add" route and see
the full employee listing on the "/employees" route.
Part 4: Adding New Routes to query "Employees"
Step 1: Update the "/employees" route
• In addition to providing all of the employees, this route must now also support the following optional
filters (via the query string)
o /employees?status=value
â–ª return a JSON string consisting of all employees where value could be either "Full Time"
or "Part Time" - this can be accomplished by calling the getEmployeesByStatus(status)
function of your data-service (defined below)
o /employees?department=value
▪ return a JSON string consisting of all employees where value could be one of 1, 2, 3, … 7
(there are cu
ently 7 departments in the dataset) " - this can be accomplished by calling
the getEmployeesByDepartment(department) function of your data-service (defined
elow)
o /employees?manager=value
▪ return a JSON string consisting of all employees where value could be one of 1, 2, 3, …
30 (there are cu
ently 30 managers in the dataset) " - this can be accomplished by
calling the getEmployeesByManager(manager) function of your data-service (defined
elow)
o /employees
â–ª return a JSON string consisting of all employees without any filter (existing functionality)
Step 2: Add the "/employee/value" route
• This route will return a JSON formatted string containing the employee whose employeeNum matches
the value. For example, once the assignment is complete, localhost:8080/employee/6 would return
the manager: Cassy Tremain - - this can be accomplished by calling the getEmployeeByNum(num)
function of your data-service (defined below).
Part 5: Updating "data-service.js" to support the new "Employee" routes
Note: All of the below functions must return a promise (continuing with the pattern from the rest of the data-
service.js module)
Step 1: Add the getEmployeesByStatus(status) Function
• This function will provide an a
ay of "employee" objects whose status property matches the status parameter
(ie: if status is "Full Time" then the a
ay will consist of only "Full Time" employees) using the resolve method of
the returned promise.
• If for some reason, the length of the a
ay is 0 (no results returned), this function must invoke the reject method
and pass a meaningful message, ie: "no results returned".
Step 2: Add the getEmployeesByDepartment(department) Function
• This function will provide an a
ay of "employee" objects whose department property matches the department
parameter (ie: if department is 5 then the a
ay will consist of only employees who belong to department 5 )
using the resolve method of the returned promise.
• If for some reason, the length of the a
ay is 0 (no results returned), this function must invoke the reject method
and pass a meaningful message, ie: "no results returned".
Step 3: Add the getEmployeesByManager(manager) Function
• This function will provide an a
ay of "employee" objects whose employeeManagerNum property matches the
manager parameter (ie: if manager is 14 then the a
ay will consist of only employees who are managed by
employee 14 ) using the resolve method of the returned promise.
• If for some reason, the length of the a
ay is 0 (no results returned), this function must invoke the reject method
and pass a meaningful message, ie: "no results returned".
Step 4: Add the getEmployeeByNum(num) Function
• This function will provide a single "employee" object whose employeeNum property matches the num
parameter (ie: if num is 261 then the "employee" object returned will be "Glenine Focke" ) using the resolve
method of the returned promise.
• If for some reason, the employee cannot be found, this function must invoke the reject method and pass a
meaningful message, ie: "no result returned".
Part 6: