javascript - Express.js - Execute function on every HTTP request -
i have function unzip() on local node.js server downloads .json.gz file aws cloudfront url , unzips .json, when server run node app.js.
the posts json file loaded before responding routes -
app.all('*', function(req, res, next){ fs.readfile('s3posts.json', function(err, data){ res.locals.posts = json.parse(data); next(); }); }); the problem file downloaded , unzipped when server starts running, , serves same data user long it's running. file not re-downloaded unless server restarted. so, changes data not reflected until restart.
i've tried calling unzip() function inside of above mentioned app.all() route before fs.readfile() function reads json file, throws error when request page -
undefined:1 undefined ^ syntaxerror: unexpected token u in json @ position 0 @ json.parse (<anonymous>) @ readfilecontext.callback (/users/anish/workspace/nodejs/unzipper/app.js:48:27) @ fsreqwrap.readfileafteropen [as oncomplete] (fs.js:359:13) when in directory, see downloaded file failed unzip it. downloaded file corrupt , can't unzipped manually.
note - file downloads, unzips , gets served fine if it's placed outside of app.all().
how can download , unzip file on every request user?
i recommend reading middlewares in https://expressjs.com/en/guide/using-middleware.html
app.use(function(request)... should deal request regardless of routing.
Comments
Post a Comment