I need to trace all "static" files served by nodejs express
I use app.use( express.static( path.join( __dirname, '/public' ) ) ) ;
Can it be done ?
I'd like to use simple "console.log( "static serving" + filename ) ;
You could create a custom middleware which checks if a request is pointing on some static asset by looking on request.path. For example:
module.exports = (request, response, next) => {
const {method, path} = request;
if (method.toLowerCase() === 'get' && path.startsWith('/public'))
console.log('static serving', path);
next();
};
Does it help?
I got some code that logs ALL requests :
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+'}.' ) ;
} ) ;
Even shorter :
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+'}.' ) ;
} ) ;
Ciao.
You can use express to write middleware for the route /public. Something like this:
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}`);
});
This code works :
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}.` ) ;
} ) ;
This code dont :
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}.` ) ;
} ) ;
Why ? I just dont know.
As you can see, the only difference is "app.use" has "/public" or not ...
It seems to me "express.static" takes it all ...