Exercise: Express Routing
Objective#
Your objective is to create a server using Express.js with Create, Read, Update, and Delete (CRUD) endpoints for interacting with mock user data.
Getting Started#
Fork and clone the node exercise repository.
Steps#
- Exercise 1: Direct Requests to Routes
- Exercise 2: Create User CRUD Routes
- Exercise 3: Finish the GET Users Route
- Exercise 4: Finish the POST Users Route
- Exercise 5: Finish the PUT Users Route
- Exercise 6: Finish the DELETE Users Route
- Exercise 7: Direct Requests to User Routes
Exercise 1: Direct Requests to Routes#
In your server.js file:
- Use a default import to import
routerfrom therouter/index.jsfile - Use
app.use()to handle all requests matching"/api"withrouter
Exercise 2: Create User CRUD Routes#
In routes/:
Create a new file named
users.route.jsImport
expressfrom the express packageImport
dbfrommockdb/(make sure to adjust the path relative to the current file)dbis a mock database service with CRUD methods to interact with the mock data
Create a variable named
router, and assign the return value fromexpress.Router()as its valueCreate a
getroute that matches the path/:id?Create a
postroute that matches the path/Create a
putroute that matches the path/:idCreate a
deleteroute that matches the path/:idFor each route, pass a second parameter that resembles:
Export
routeras the default export
We will direct paths already matching
"/api/users"to these routes
Exercise 3: Finish the GET Users Route#
In the try block of the request handler for GET requests, set up the following:
- Destructure
idfromreq.paramsreq.paramsis an object that contains the parsed key/value pairs from the url parameters
- Declare a variable named
data- do not assign it a value
- Create an
if/elsestatement that checks ifidis truthy- if it is,
datashould be the result ofawait db.getOne(id) - if it isn't,
datashould be the result ofawait db.getAll()
- if it is,
- Send
dataas the json response to the request
Exercise 4: Finish the POST Users Route#
In the try block of the request handler for POST requests, set up the following:
- Declare a variable named
newUserand assign it the value ofreq.bodyreq.bodyis an object that contains the parsed request body as JSON
- Declare a variable named
dataand assign it the value ofawait db.add(newUser) - Send
dataas the json response to the request
Exercise 5: Finish the PUT Users Route#
In the try block of the request handler for PUT requests, set up the following:
- Destructure
idfromreq.paramsreq.paramsis an object that contains the parsed key/value pairs from the url parameters
- Declare a variable named
updatedUserand assign it the value ofreq.bodyreq.bodyis an object that contains the parsed request body as JSON
- Declare a variable named
dataand assign it the value ofawait db.update(id, updatedUser) - Send
dataas the json response to the request
Exercise 6: Finish the DELETE Users Route#
In the try block of the request handler for DELETE requests, set up the following:
- Destructure
idfromreq.paramsreq.paramsis an object that contains the parsed key/value pairs from the url parameters
- Declare a variable named
dataand assign it the value ofawait db.remove(id) - Send
dataas the json response to the request
Exercise 7: Direct Requests to User Routes#
In routes/index.js:
- Import the router as
userRouterfromusers.route.js - Use
router.use()to handle all requests matching"/users"withuserRouter
Test your routes with the browser or postman. If there are any errors, go back to users.route.js and see if you can resolve the issues. Happy Hacking!
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out:
- Express Routing Exercise Repo on Github