如何跟踪nodejs表达服务静态文件

时间:2019-04-08 19:01:29

标签: node.js express

我需要跟踪nodejs express服务的所有“静态”文件

我用 app.use(express.static(path.join(__dirname,'/ public'))));

可以做到吗?

我想使用简单的“ console.log(”静态服务“ +文件名);

4 个答案:

答案 0 :(得分:0)

您可以创建一个自定义中间件,该中间件可以通过查看request.path来检查请求是否指向某个静态资产。例如:

module.exports = (request, response, next) => {

    const {method, path} = request;
    if (method.toLowerCase() === 'get' && path.startsWith('/public'))
        console.log('static serving', path);

    next();
};

有帮助吗?

答案 1 :(得分:0)

您可以使用express为路由/public编写中间件。像这样:

const express = require('express');
const path = require('path');
const app = express();

const PORT = 3000;

app.use('/public', (req, res, next) => {
    const { url, path: routePath }  = req;
    console.log(url);
    // or
    console.log(routePath);
    next();
});

app.use(express.static(path.join(__dirname, '/public')));

app.listen(PORT, () => {
    console.log(`app is running on ${PORT}`);
});

答案 2 :(得分:0)

我有一些记录所有请求的代码:

const express = require('express') ;
const path    = require('path') ;
const app     = express() ;

const PORT = 3000 ;

var myLogger = function (req, res, next) {

  const { url, path: routePath }  = req ;
  console.log( 'my LOGGER - URL (' + url + '), PATH (' + routePath + ').' ) ;

  next() ;
}

app.use(myLogger) ;

app.use( express.static( path.join( __dirname, '/public') ) ) ;

app.listen(PORT, () => {
  console.log( 'app is running on port {'+PORT+'}.' ) ;
} ) ;

更短:

const express = require('express') ;
const path    = require('path') ;
const app     = express() ;

const PORT = 3000 ;

app.use( function ( req, res, next ) {
  console.log( '### common TimeStamp:', Date.now() ) ;
  next() ;
} ) ; // timestamp all

app.use( express.static( path.join( __dirname, '/public') ) ) ;

app.listen(PORT, () => {
  console.log( 'app is running on port {'+PORT+'}.' ) ;
} ) ;

曹。

答案 3 :(得分:0)

此代码有效:

const express = require('express') ;
const path    = require('path') ;
const app     = express() ;
const PORT = 3000 ;

app.use( function ( req, res, next ) {
    const { url, path: routePath }  = req ;
    console.log( '### common TimeStamp:', Date.now(), ' - my LOGGER - URL (' + url + '), PATH (' + routePath + ').' ) ;
    next() ;
} ) ; // timestamp all

app.use( express.static( path.join( __dirname, '/public' ) ) ) ;

app.listen(PORT, () => {
  console.log( `app is running on port ${PORT}.` ) ;
} ) ;

此代码不:

const express = require('express') ;
const path    = require('path') ;
const app     = express() ;
const PORT = 3000 ;

app.use( '/public', function ( req, res, next ) {
    const { url, path: routePath }  = req ;
    console.log( '### common TimeStamp:', Date.now(), ' - my LOGGER - URL (' + url + '), PATH (' + routePath + ').' ) ;
    next() ;
} ) ; // timestamp all

app.use( express.static( path.join( __dirname, '/public' ) ) ) ;

app.listen(PORT, () => {
  console.log( `app is running on port ${PORT}.` ) ;
} ) ;

为什么?我只是不知道。

如您所见,唯一的区别是“ app.use”是否具有“ / public” ...

在我看来,“ express.static”可以容纳一切……