Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Express.js - Execute function on every HTTP request

I have a function unzip() on my local Node.js server that downloads a .json.gz file from an AWS CloudFront URL and unzips it to .json, when the server is run with node app.js.

The posts from that json file are loaded before responding to routes -

app.all('*', function(req, res, next){
    fs.readFile('s3posts.json', function(err, data){
        res.locals.posts = JSON.parse(data);
        next();
    });
});

The problem is that the file is downloaded and unzipped when the server starts running, and serves that same data to the user as long as it's running. The file is not re-downloaded unless the server is restarted. So, any changes to that data is not reflected until the restart.

I've tried calling the unzip() function inside of the above mentioned app.all() route before the fs.readFile() function that reads from the json file, but that throws an error when I request any page -

undefined:1
undefined
^

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at ReadFileContext.callback (/Users/Anish/Workspace/NodeJS/unzipper/app.js:48:27)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:359:13)

When I look in the directory, I see that it downloaded the file but failed to unzip it. Even the downloaded file is corrupt and can't be unzipped manually.

NOTE - The file downloads, unzips and gets served fine if it's placed outside of app.all().

How can I download and unzip the file on every request from the user?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I recommend reading about middlewares in https://expressjs.com/en/guide/using-middleware.html

app.use(function(request)... should help you deal with all request regardless of the routing.

about 4 years ago · Santiago Trujillo Report

0

I was able to use the request module to do this -

app.all('*', function(req, res, next) {
    request({
        method: 'GET',
        uri: 'http://post.s3post.cf/s3posts.json.gz',
        gzip: true
    }, function(error, response, body) {
        res.locals.posts = JSON.parse(body);
        next();
    });
});

This also eliminated the need for me to have a custom unzip() function that downloaded and unzipped the file.

about 4 years ago · Santiago Trujillo Report

0

You should try this,

app.all('*', function(req, res, next){
    fs.readFile('http://post.s3post.cf/s3posts.json.gz', function(err, data){
        res.locals.posts = JSON.parse(data);
        next();
    });
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!