Skip to content

Instantly share code, notes, and snippets.

View jkantr's full-sized avatar

Jared Kantrowitz jkantr

  • The Witzend Group
  • Metro NY Area
View GitHub Profile
const CronJob = require('cron').CronJob
// run once a day at midnight (0 minute of 0 hour, every day/week/month)
new CronJob('0 0 * * *', () => {
getAllStudents.clear()
}, null, true)
@jkantr
jkantr / api.js
Last active July 6, 2017 04:05
Mounted Routers as Parametric Modules
const express = require('express')
const router = express.Router()
module.exports = (Knex) => {
router.get('/', (req, res) => {
// now you have Knex object in scope!
res.render('main', { title: 'Main' })
})
@jkantr
jkantr / .md
Last active September 18, 2018 21:40
Fun with Express patterns

Express patterns

Parametric modules

This pattern takes advantage of lexical scope in javascript, CommonJS modules, and object destructuring. It's a good way to share stateful resources, such as a db connection object, to middleware and detached routers in Express.

Basic setup

/app.js:

const express = require('express')
const checkStatus = (response) => {
if (response.status >= 200 && response.status < 300) return response
const error = new Error(`Error ${response.status}: ${response.statusText}`)
error.response = response
throw error
}
const parseResponse = (response) => {
const authToken = response.headers.get(`Authorization`)
return response.json().then(json => ({ authToken, json }))
@jkantr
jkantr / fizzbuzz.md
Last active March 1, 2019 02:55
"Functional" Javascript FizzBuzz solution
Array.from({ length: 100 }, (v, i) => [[15, 'fizzbuzz'], [5, 'buzz'], [3, 'fizz']].find(x => (i+1) % x[0] === 0) || (i + 1))
  .reduce((a, o) => a += (o[1] || o) + "\n",'')
@jkantr
jkantr / .md
Last active April 23, 2017 19:28
Cartesian product of N arrays in Javascript
const cartesianProduct = (...args) => args.reduce((a, b) => a.map(x => b.map(y => x.concat([y]))).reduce((acc, t) => acc.concat(t), []) , [[]])
const fetch = require('node-fetch')
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
var error = new Error(response.statusText)
error.response = response
throw error
}
@jkantr
jkantr / .js
Created June 8, 2017 16:36
Fold large array into an array of arrays of N length
const foldArr = (arr, groupSize) => arr.reduce((a, b, i, g) => !(i % groupSize) ? a.concat([g.slice(i, i + groupSize)]) : a, []);
import requestLanguage from 'express-request-language';
import models, { Locale, User, UrlManager } from './data/models';
app.use(async (res, req, next) => {
const locales = await Locale.findAll({}).then(locales => locales.map(loc => loc.locale));
//app.set('locales', locales);
req.locales = locales;
next();
@jkantr
jkantr / .js
Last active January 2, 2021 02:58
const Promise = require('bluebird');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const getAllUrls = () => {
return fetch('http://regioni.usyouthsoccer.org')
.then(res => res.text())
.then((html) => {
const $ = cheerio.load(html);
return $('#associationscarousel').find('> li > a').toArray().map(link => $(link).attr('href'))