Let’s learn how to register a user without a database. In a real production environment, you should use a database, but we’ve simplified this example to focus on the creation of new users.

Instead of a database, we’ll use an array holding data:

// users: let records = [ { id: 1, username: "sam", password: "[email protected]", }, { id: 2, username: "jill", password: "[email protected]!", }, ];

Using a custom helper function we created in users.js, we can retrieve user data upon registration and update the records array:

function createUser(user) { return new Promise((resolve, reject) => { const newUser = { // getNewId creates an updated ID // for the new user id: getNewId(records), ...user, }; records = [newUser, ...records]; resolve(newUser); }); };

In the createUser() function, we’re creating a Promise in order to prevent events from becoming blocked when running the application.

createUser() creates a new user and inserts them into our database, records. Once created, we resolve the Promise and send back the newly created user. If you need a refresher on promises, take a look at our Promises cheat sheet for a quick overview.

NOTE: Since we’re not making use of a database, the newly created user will not be persisted if the server restarts.


Now we can make use of this helper function in our endpoint to create users, let’s move on to the next exercise to see how to accomplish that!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?