Exercise: Express Routing
#
ObjectiveYour objective is to create a server using Express.js with Create, Read, Update, and Delete (CRUD) endpoints for interacting with mock user data.
#
Getting StartedFork 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 RoutesIn your server.js
file:
- Use a default import to import
router
from therouter/index.js
file - Use
app.use()
to handle all requests matching"/api"
withrouter
#
Exercise 2: Create User CRUD RoutesIn routes/
:
Create a new file named
users.route.js
Import
express
from the express packageImport
db
frommockdb/
(make sure to adjust the path relative to the current file)db
is 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
get
route that matches the path/:id?
Create a
post
route that matches the path/
Create a
put
route that matches the path/:id
Create a
delete
route that matches the path/:id
For each route, pass a second parameter that resembles:
Export
router
as the default export
We will direct paths already matching
"/api/users"
to these routes
#
Exercise 3: Finish the GET Users RouteIn the try block of the request handler for GET requests, set up the following:
- Destructure
id
fromreq.params
req.params
is 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/else
statement that checks ifid
is truthy- if it is,
data
should be the result ofawait db.getOne(id)
- if it isn't,
data
should be the result ofawait db.getAll()
- if it is,
- Send
data
as the json response to the request
#
Exercise 4: Finish the POST Users RouteIn the try block of the request handler for POST requests, set up the following:
- Declare a variable named
newUser
and assign it the value ofreq.body
req.body
is an object that contains the parsed request body as JSON
- Declare a variable named
data
and assign it the value ofawait db.add(newUser)
- Send
data
as the json response to the request
#
Exercise 5: Finish the PUT Users RouteIn the try block of the request handler for PUT requests, set up the following:
- Destructure
id
fromreq.params
req.params
is an object that contains the parsed key/value pairs from the url parameters
- Declare a variable named
updatedUser
and assign it the value ofreq.body
req.body
is an object that contains the parsed request body as JSON
- Declare a variable named
data
and assign it the value ofawait db.update(id, updatedUser)
- Send
data
as the json response to the request
#
Exercise 6: Finish the DELETE Users RouteIn the try block of the request handler for DELETE requests, set up the following:
- Destructure
id
fromreq.params
req.params
is an object that contains the parsed key/value pairs from the url parameters
- Declare a variable named
data
and assign it the value ofawait db.remove(id)
- Send
data
as the json response to the request
#
Exercise 7: Direct Requests to User RoutesIn routes/index.js
:
- Import the router as
userRouter
fromusers.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 LinksIf 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