Learn

Let’s use the createUser() helper function in our routes. We’ll add the logic to create users in a POST request to "/register".

Since we’re working with promises, we can create an asynchronouse route handler by using async/await:

app.post("/register", async (req, res) => { ... })

We’ll retrieve the user data from req.body and await as we call our helper function to create the new user:

app.post("/register", async (req, res) => { const { username, password } = req.body; // imported helper function: // db.users.createUser const newUser = await db.users.createUser({ username, password });

If a newUser is successfully created, we send a status code of 201 and a json response back to the client:

app.post("/register", async (req, res) => { const { username, password } = req.body; const newUser = await db.users.createUser({ username, password }); if (newUser) { res.status(201).json({ msg: "Insert Suceess Message Here", insertDataHere }); }

Lastly, we want to handle potential errors that might occur. In an else statement we can return a status code of 500 indicating that there was a relevant error message:

app.post("/register", async (req, res) => { const { username, password } = req.body; const newUser = await db.users.createUser({ username, password }); if (newUser) { res.status(201).json({ msg: "Insert Success Message Here", insertDataHere }); } else { res.status(500).json({ msg: "Insert Failure Message Here" }); }

NOTE: In a real development environment, passwords would be hashed whenever a new user registers.

With the route completed, users will now be able to register and log in!

Instructions

1.

At the moment, the app can’t be run since it’s missing some logic.

Scroll to the bottom of the app.js file where you’ll find a post request to "/register".

Start by making a call to the provided helper function, db.users.createUser(), using the correct arguments.

Store the result in a const variable called newUser.

Type node app.js into the Terminal to start the node app.

Press the Check Work button to check your work for each checkpoint.

2.

We want to check if a user was successfully created. Add the correct condition to the provided if/else statement.

3.

If the new user was successfully created, send back a status code of 201 along with a json response object with the following key-value pairs:

  • msg: A message indicating a user was created.
  • The newly created user using the object literal shorthand with newUser.
4.

Handle the potential error in the else statement by sending back a status code of 500 and a json response with the following information:

  • msg: A message indicating a user wasn’t created.
5.

If you haven’t already, in the terminal run the command:

node app.js

Navigate to the http://localhost:8000/register path in the mini-browser and attempt to register a new user.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?