how to log the file requested via express.static

前端 未结 3 1107
傲寒
傲寒 2021-02-07 04:51

here is my code

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

it i

3条回答
  •  自闭症患者
    2021-02-07 05:16

    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);
    

提交回复
热议问题