UNPKG

@tlrg/middleware-js

Version:
31 lines (28 loc) 967 B
var rp = require('request-promise'); var logger = require('tlrg-logger')(); function setCookie (res, cookieVal) { var cookieOpts = { maxAge: 86400 // 1 day }; res.cookie('PACookie', cookieVal, cookieOpts); } module.exports = function createPACookie (req, res, next) { // If cookie doesn't exist, set cookie. if (!req.cookies.PACookie) { rp('https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new') .then(function (htmlString) { setCookie(res, htmlString); req.moonstick.predictiveAnalytics = htmlString; next(); }) .catch(function (err) { // deal with error logger.info('Error setting PACookie', err); next(); }); } else { var cookieVal = req.cookies.PACookie; req.moonstick.predictiveAnalytics = cookieVal; next(); } };