title | slug | description | keywords | ||||
---|---|---|---|---|---|---|---|
Exercise: Express Routing |
/express-routing-exercise |
Express Routing Exercise with Steps |
|
Your objective is to create a server using Express.js with multiple CRUD endpoints for interacting with mock user data.
Fork and clone the node exercise repository.
- 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
In you 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
In routes/
:
- Create a new file named
users.route.js
- Import
express
from the express package - Import
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 value - Create 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
- Export
router
as the default export
We will direct paths already matching
"/api/users"
to these routes
In 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
In 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
In 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, newUser)
- Send
data
as the json response to the request
In 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
In 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!
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