Migraine Tracker – Create Backend

I have long suffered with migraines.  Currently I track my daily migraines on an Excel spreadsheet, but I am going to create my own app to track them using the MERN stack Node, Express, MongoDB and Mongoose on the server.  I will develop the client in React.

  1. npm init -y to create my package.json file
  2. create index.js – I import Express to create my server.  I also bring in CORS middlesware, which I will need later to handle requests from the client.  I also have Morgan for loggin HTTP requests, and body-parser for parsing JSON data.  The dotenv module is for reading the .env file.



Next I’ll handle authentications.  First I’ll create my mongoose model.  I’ll use bcrypt for hashing the password.

  1. I created my User model for mongoose
  2. auth handler
  3. auth middleware
  4. auth route

For the Auth handler, I store the mongoose models in db, and load jsonwebtoken into jwt to hash a password for a new user, or to compare a password to the stored hash for an existing user.

The Auth middleware loginRequired makes sure the user is logged in before they can input new data or access existing data.  I use “dotenv” to access the .env file for the SECRET_KEY.  I use jsonwebtoken to verify the user.

Below I create the Auth routes for signup and signin.

Next, I test that the signup route is working, which it seems to be.  If I login, it returns with a JSON web token, which is the behavior I’m expecting.


  1. I created my myHeadaches model for mongoose
  2. myHeadaches handler
  3. headaches route

Handlers – In the myHeadaches handler, I create functions createHeadache, getHeadache, deleteHeadache, and getAllHeadaches to create, delete, and get a list of all headaches.

Routes – Here are my routes for creating, deleting and retrieving all records.

I tested posting a new entry, and it works.

And deleting a post also works.

Now I’m ready to move onto the frontend.

Leave a Reply

Your email address will not be published.