How to Create a Nodejs App Part 4: Enable Server Registration

Code Diff
New Code Base

The Goal

Today I wanted to write the backend code so that when a teacher registers, that teacher will be added to the database. If you’ll recall, I’ll be using MongoDB as my DB. As I don’t intent to have the server stay up, I’ve left out most of the security, but with a little research you can figure out how to do that. So let’s get started!

Mongoose

Mongoose is an awesome OMD library that makes things ohh so very simple, so that’s what I’ll be using!  First thing I need to go is update the package to include the Mongoose library. You’ll notice that I also added another dependency called “when” which I’ll get into later.

Next I need to set up the connection to the data. In my case I want the server startup to fail if the connection to the DB can’t be established. So I only start the server when the db ‘open’ event has been caught.

 

Teacher Schema

So now that we can connect to the database, I’ll make a schema! Mongoose works but creating a model out of a schema. The schema is just a object representation of what you want to store, in this case a teacher. My teacher object will have the following attributes. fName, lName, email,id, classes, students,username, and password. I’ve added a created/updated too. Once I have the schema, I make a model out of the schema and export that model so that other modules can have access to it.

Now I will create an teacher api that will execute methods on the teacher mondel. For now all I need is a save.

You’ll notice that the save method wraps the save logic inside a promise. This come from that when dependency that I mentioned above. You can read about it by following the link, but basically what it does is that it tells the function that called the current that I will let you know when I am done. Here the resolve says that I saved the teacher successfully and the reject says that an error occurred and returns that error.  In other words, I “promise” to let you know when I am done doing what I said I do.

Next I have to update the function that handles the POST request to register a new teacher. 

The .then tells the method to wait for the called method to return a resolve event. The .catch likewise listens for a reject event. So if I get a resolve I know the save succeeded and I send a 200 response to the client. If I get a reject, I know it failed and I send a 500.

There are two other new additions that I didn’t go over, but that you can find then in the links at the top. The first is a new crypt.js module that encrypts the users password using NodesJS’s Crpto module. The second is a utilFunctions.js that will hold any thing that I think might be useful. For now all it has is a function that returns a random string of 5 characters that I use as a teacher ID. Now let’s test it!

Here the collection is empty.

Now I create a new teacher.

And I check the database again.

Success!!!

Thanks for reading! Next time I’ll be implementing the login! 

Facebooktwittergoogle_plusredditpinterestlinkedinby feather