簡體   English   中英

如何跟蹤nodejs表達服務靜態文件

[英]how to trace nodejs express serving static files

我需要跟蹤nodejs express提供的所有“靜態”文件

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

可以嗎?

我想使用簡單的“console.log(”靜態服務“+文件名);

您可以創建一個自定義中間件,通過查看request.path來檢查請求是否指向某個靜態資產。 例如:

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

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

    next();
};

有幫助嗎?

您可以使用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}`);
});

我得到了一些記錄所有請求的代碼:

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+'}.' ) ;
} ) ;

再見。

此代碼有效:

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”可以把它全部......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM