How to register a file requested via express.static

here is my code

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

it serves the static file correctly

I want to register when a file with the extension .xls is requested

how can i achieve this

+8
express
source share
2 answers

The main path module gives you tools to solve this problem. So just put this logic in the middleware in front of your static middleware, for example:

 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); 
+9
source share

Just do

 var express=require("express"); var app=express(); var port=8181; app.use(function(req, res, next) { // check for .xls extension console.log(req.originalUrl); next(); }, express.static(__dirname)); app.listen(port); 
+1
source share

All Articles