Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to log the file requested via express.static

Tags:

here is my code

var express=require("express"); var app=express(); var port=8181; app.use(express.static(__dirname)); app.listen(port); 

it is serving static file properly

I want to log when a file with an extension .xls is being requested

how can i achieve it ?

like image 870
ahhmarr Avatar asked Mar 31 '15 12:03

ahhmarr


1 Answers

The path core module gives you the tools to deal with this. So, just put this logic in a middleware before your static middleware, like:

var express = require("express"); var path = require("path");  var app = express(); var port = 8181;  app.use(function (req, res, next) {     var filename = path.basename(req.url);     var extension = path.extname(filename);     if (extension === '.css')         console.log("The file " + filename + " was requested.");     next(); }); app.use(express.static(__dirname));  app.listen(port); 
like image 82
Rodrigo Medeiros Avatar answered Oct 14 '22 00:10

Rodrigo Medeiros