I am trying to get this code below run as middleware from an exported function.
fs.stat("./tmp/my_data.json", (err, stats) => {
scraperDate = stats.mtime.toUTCString();
});
I have
let scraperDate = "";
in top of my routes file. Im trying to do this:
router.get("/", myFunctions.scraperDateFunction, function (req, res) {
res.render("my_view", {scraped: scraped, scraperDate: scraperDate});
});
When I just run the scraperDate code as it is, above the route, it works. When I put it in my functions.js file to module.exports it wont write to the scraperDate variable.
I am probably missing something obvious here, but I have tried to get this to work for the better part of two days now. This is my module.exports for the function:
module.exports = {
    scraperDateFunction: function(){
        fs.stat("./tmp/my_data.json", (err, stats) => {
            scraperDate = stats.mtime.toUTCString();
        });
    }
}
* Edit *
I have now tried this
getScrapeDate: function(req, res, next){
    fs.stat("./tmp/my_data.json", (err, stats) => {
        scraperDate = stats.mtime.toUTCString();
        console.log(err)
        console.log(stats)  
        return next;        
    });
}
which prints the  stats  as expected to console withour any errors. It is likely something with the scope. How would I pass the result of  stats.mtime.toUTCString();  to the  scraperDate  variable in the route?
* Edit 2 *
I now have this in my functions file
    getScrapeDate: function(req, res, next){
    fs.stat("./tmp/my_data.json", (err, stats) => {
        if (err) {
            next (err);
        } else {
            res.locals.scraperDate = stats.mtime.toUTCString()
        }
    });
}
and this in my routes file as suggested but it does not load my view
router.get("/", myFunctions.getScrapeDate, function (req, res) {
    let {scraperDate} = res.locals;
    res.render("my_view", {scraped: scraped, scraperDate: 
    scraperDate});
});
scraped is declared in the top of the routes file.
* Final Edit *
This is a setup that is now working
router.get("/", myFunctions.getScrapeDate, function (req, res) {
    let {scraperDate} = res.locals;
    res.render("my_view", {scraped, scraperDate});
 });
and
    getScrapeDate: function(req, res, next){
    fs.stat("./tmp/my_data.json", (err, stats) => {
        if (err) {
            next (err);
        } else {
            res.locals.scraperDate = stats.mtime.toUTCString();
            next();
        }                   
    });
}
 
     
    