Part 6: Emails, cronjobs & deploying!
Recap of Last Time
- We built a Node.js app utilising the built-in http.createServer
- Starts an HTTP server and serves static files out of the ./public directory
- We implemented dynamic routing with a handy easy to define syntax
- Our routes use Handlebars template files to display content that's not static
- We also wrote a super simple form data parser that will allow us to capture user data
- We connected up a database, using SQL queries mixed into "actions" to contain them
- We made a pipeline making it easier for us to keep our frontends nice
What are we building again?
Ever wanted to remind yourself of something, out of the blue? Well, we're building a quick tool to do that!
What's it all for?
- To start, this course was to prove that
you don't need a framework.
- Sometimes, not thinking properly about your architectural choices can lead you down a wrong path. Always consider the true cost of a framework!
- Don't avoid frameworks just because either – they serve a very useful purpose.
- An MVP should be simple. Simplify your MVP build by focusing away from learning/working in frameworks!
What we're going to do
- We need to connect our app to Mailgun.
- A background task (cronjob) needs to be made to run our app's sender.
- We deploy to Heroku!
Let's get started with the mailer
We're going to use Mailgun, but any transactional provider will do.
- You will need to go to mailgun.com, create an account.
- You will also probably need to set up the MX/DKIM records on a domain name you own
We're already using Handlebars...
We're already using Handlebars.js in our app's views, so may as well use it in emails too!
(reduce, re-use, recycle)
Let's create our email
Emails: Loading the files in
Emails: the compile function
Emails: sending via mailgun
Now, we need a way to send them...
A daily task designed to be run in the server environment will send the mails for us by using our compile and send functions!
The magic task...
Time to execute the task
A "cronjob" is the simplest way of doing this:
* Or, we could use Heroku's scheduler
What is heroku?
- Heroku is a hosting platform. Their hosting has an emphasis on the 12 factor app, so it's perfect for us!
- They have a free tier so you can tinker all you want before launch.
- They have a number of free addons built in that make your life easier.
- In the long run though, if you want to learn to do deployments on your own or if cost is a factor, you will want to migrate away from Heroku, especially at scale.
Heroku deployment checklist
- Create a Heroku account
- Create an app
- Provision logging, a Postgres database and a Heroku Scheduler
- If your code is on Github, sync the repo with the app, otherwise use Heroku's CLI
- Create a CNAME record under your domain and get launching!
Well, it's been fun
I guess...like...we're kinda done?
Congrats! You built it!
I guess we've done it!
It doesn't have to end – I am going to gather feedback, see what people want, and then maybe continue. Let me know what you think please!
See today's code: https://github.com/frameworkless-js/remind.ist/tree/stage/6
See today's lesson running live: https://remind.ist
frameworkless.js -> Part 6
By Mike Timofiiv