I have variables 'startDate' and 'endDate' which have the same definition across all 10 routes I have in my controller (NodeJS / Express).
How can I declare these globally so they don't have to be declared numerous times in each route?
My current code checks to see if startDate has been sent as part of the request from the page, otherwise sets the startDate using momentJS.
Currently, If I simply put the variables above the routes I get an error stating .req is not defined.
I'm fairly new to JS so any help would be appreciated.
Cheers
var express = require('express');
var router = express.Router();
var ccmQueuePerformance = require('../models/ccmQueuePerformance');
var moment = require('moment');
// Get hourly data by queue
router.get('/callsOfferedByQueue', async function(req, res, next) {
try {
// Declare startDate and endDate
var startDate = moment().startOf('month').format('YYYY-MM-DD');
if(req.query.startDate) startDate = moment(req.query.startDate, 'YYYY-MM-DD').startOf('day');
var endDate = moment().endOf('month').format('YYYY-MM-DD');
if(req.query.endDate) endDate = moment(req.query.endDate, 'YYYY-MM-DD').startOf('day');