Book Image

Express.js Blueprints

By : Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang
Book Image

Express.js Blueprints

By: Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang

Overview of this book

<p>APIs are at the core of every serious web application. Express.js is the most popular framework for building on top of Node.js, an exciting tool that is easy to use and allows you to build APIs and develop your backend in JavaScript. Express.js Blueprints consists of many well-crafted tutorials that will teach you how to build robust APIs using Express.js.</p> <p>The book covers various different types of applications, each with a diverse set of challenges. You will start with the basics such as hosting static content and user authentication and work your way up to creating real-time, multiplayer online games using a combination of HTTP and Socket.IO. Next, you'll learn the principles of SOA in Node.js and see them used to build a pairing as a service. If that's not enough, we'll build a CRUD backend to post links and upvote with Koa.js!</p>
Table of Contents (14 chapters)

Periodical tasks with node-cron


Maybe you are familiar with cron (http://en.wikipedia.org/wiki/Cron). It's a Unix-based task scheduling system that makes running tasks easy. One problem with it is that it's linked to your platform, and it's not trivial to turn it on and off from code.

Meet node-cron (https://github.com/ncb000gt/node-cron). It's basically the same task scheduler but it runs directly from your Node application, so as long as it is up, your jobs should run.

Our strategy is simple: Periodically select all meetings that need mailing, call our mailer with each of these meetings, and then mark it as emailed.

Following this app's convention, let's separate concerns into their own folders, starting with src/tasks/index.js, as shown in the following code:

var CronJob = require('cron').CronJob;

module.exports = function(models, mailer) {
  var tasks = {};

  tasks.followupMail = require('./followupMail')(models,mailer);

  tasks.init = function() {
    (new CronJob('00 */15 * * * *',...