In this module, you'll see how we're using Redis Hashes to model the user and location data in our application.
In your first coding exercise, you'll be adding a new route that takes a user's ID and returns their full name.
Using your IDE, open the
node-js-crash-course folder that you cloned the GitHub repository into. Open the file
src/routes/user_routes.js and find the route
/user/:userId/fullname which looks like this:
In this exercise, you'll modify the code to return the user's full name by retrieving the firstName and lastName fields for the requested user from Redis.
First, make sure your server is still running, if not start it with:
Next, browse to
You should see:
Take a look at the documentation for the Redis HMGET command, which retrieves multiple named fields from a Redis Hash. You'll need to add code that calls the Redis client's
hmget function, then place the values returned into the
lastName variables. You should be able to retrieve both values using a single call to
hmget. For guidance on how to invoke Redis commands, check out the code for the
/user/:userId route which calls the
nodemon will restart the server automatically for you each time you save your changes.
When you're ready to test your solution, browse to
http://localhost:8081/api/user/5/fullname and you should see:
If you need help from our team, join us in Discord.
In this video, Justin explains what Redis Hashes are and shows how common Redis Hash commands work:
You can find documentation for all of the Redis Hash commands on redis.io.