Coding Journal

Jordan Vidrine

Daily Coding Journal

An experienced business owner + musician with a demonstrated history of management in the Energy sector and an insatiable appetite for learning new and complex skills. Transitioning from an ownership role & seeking employment in Web Development as a Jr Full Stack Developer.

August 19th 2019

Today we will be learning how to send emails inside of nodeJS, using env variables (which I learned a couple weeks back), creating a production MongoDB database, as well as getting into testing inside of nodeJS. (this is something I am eager to get into, as there hasnt been any debugging yet)

Sending emails with nodeJS

We will be using SendGrid. This service is run by Twilio, which I got familiar with during my SMS Text/YNAB app I worked on a couple of weeks ago. The setup for sendgrid was extremely simple. Here is an example of it working.

const sgMail = require('@sendgrid/mail')

const sendGridAPIKey = "yourKeyHere"

sgMail.setApiKey(sendGridAPIKey)

sgMail.send({
  to: 'jordan@jordanvidrine.com',
  from: 'jordan@jordanvidrine.com',
  subject: 'first sendgrid email',
  text: 'From nodeJS'
})

We are going to be using this to send ‘users’ and email when they have signed up or canceled our theoretical service. Here is how that implementation looks. In the src folder, we create an emails folder, and add this code into ‘account.js’:

const sgMail = require('@sendgrid/mail')

const sendGridAPIKey = "key"

sgMail.setApiKey(sendGridAPIKey)

const sendWelcomeEmail = (email, name) => {
  sgMail.send({
    to: email,
    from: 'jordan@jordanvidrine.com',
    subject: 'Welcome to Our App!',
    text: `Hello ${name}! We are so glad you decided to join in on this movement.`
  })
}

module.exports = {
  sendWelcomeEmail
}

To integrate this with the User router for creating a new account we require the function we just created and add sendWelcomeEmail(user.email, user.name) to the try/catch block in the create user route.

Local Env Variables

I’ve worked with these when I was creating the YNAB/SMS app. It’s a good habit to get into for security and helps for customization of variables as well. For example, we can use certain variables in development and others in production.

For safety purposes it is important as well. Especially with git repo’s. Something I have learned is that even if you remove all API keys, logins, etc before you push to git, the data can still be accessed in the commit history.

In this current project, we will create a config folder with a dev.env file. This is where we will provide the variables for the dev environment. The syntax for our environment variables looks like this: key=value.

To use the .env file to create environment variables, we can use an NPM module called env-cmd. In the past, I have used dotenv. Dotenv has way more weekly downloads, so that seems to mean its more widely used.

Since we will only need this as a dev dependency, we can install using --save-dev.

We will be editing the package.json file dev script section to use the env-cmd to get the environment variables we have set.

"scripts": {
  "start": "node src/index.js",
  "dev": "env-cmd -f ./config/dev.env nodemon src/index.js"
}

This process is simple and causes the environment variables to be set any time we run “npm run dev”

Production with Heroku + Env Variables

This lesson also covered something I have already done, which is setting up a cloud based MongoDB server using MongoDB Atlas.

We will be pushing our current code to Git and Heroku. For the app to work correctly on Heroku we need to be able to declare environment variables inside the heroku env.

Creating Heroku app

After creating a git repository of the files, in the terminal run heroku create app {app-name}

We will then create the environment variables using heroku config. We can use this command to set, read, and delete values.

To create, in the command terminal use: heroku config:set key=value

DONT FORGET TO URL ENCODE ANY PASSWORDS THAT HAVE SPECIAL CHARACTERS!!. I did not do this at first and the app was not connecting to the database.

Summary

Thus far in the course, I have learned how to create a full fledged app that allows users to create accounts, and create “tasks” or anything for that matter under their account. This essentially is the base build for most apps that are out there today.

Instagram for examples is primarily a database of user accounts, and a list of images (tasks) that each user has created.

Rest of the day

I read through chapter 15 of Eloquent Javascript a couple times. The chapter is on creating a basic platform game in the browser only using Javascript. The level of difficulty on this chapter I feel has increased 10 fold from any precious chapter. I will continue to go through it, but it is a tedious and hard chapter for my current level to be going through.

Back to blog...