Track and log specific route requests with ExpressJS morgan feature

Hello, I am currently utilizing the NodeJS web framework Expressjs along with a middleware called morgan to log requests to a file.

Here is my configuration:

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/logs/access.log', {flags: 'a'})

// setup the logger
app.use(logger('short', {stream: accessLogStream}))

My current log entries look like:

192.168.1.3 - GET /signup HTTP/1.1 304 - - 19.194 ms 
192.168.1.3 - GET /assets/css/admin/module.admin.stylesheet-complete.sidebar_type.collapse.no_min2.css HTTP/1.1 304 - - 15.500 ms 
192.168.1.3 - GET /assets/components/library/jquery/jquery.min.js?v=v1.0.3-rc2&sv=v0.0.1.1 HTTP/1.1 304 - - 14.244 ms 

I am looking for a way to only log the route request, for example, if a user accesses /signup/:

192.168.1.3 - GET /signup HTTP/1.1 304 - - 19.194 ms

and exclude logging the assets required for that route.

Thank you in advance.

Answer №1

Encountered a similar issue and discovered a straightforward solution that may benefit others. By rearranging the order of middleware functions, such as placing express.static above the morgan call, like so:

app.use(express.static(__dirname + '/public'));
...
app.use(logger('short'));

Static assets will not be logged. Keep in mind that your asset files must actually exist, otherwise they will still be logged (verified in express 4.12).

Answer №2

If the logger is instantiated after declaring static routes, the static routes will not be logged.

app.use(express.static('...')); // declare static folder first

app.use(logger('any format here')); // instantiate logger

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The upper section of the pop-up modal is not fully visible when the screen size is decreased

My modal is experiencing an issue where the top part gets cut off when the screen is reduced, disappearing under the bookmarks and address bar. I attempted a solution mentioned in this resource by setting the top and bottom to 0, but it did not resolve the ...

Integrating Node.js into a Grails application

Apologies if I sound inexperienced. I primarily work on front-end development. Currently, I am tasked with designing new mobile views for a Grails app. My plan is to utilize Reactjs for the front-end development, but I am unsure about incorporating Nodejs ...

Having trouble retrieving the data associated with a specific ID from my datatable

I am looking to specifically load the UserData that belongs to the correct AdminId In this code snippet, all the UserData is loaded successfully. It's working fine. async mounted() { this.userData = (await DataService.index()).data; } Now, I wa ...

What is the correct way to serve JSON files with appropriate MIME type and avoid using text/plain?

My attempts to utilize a manifest.json file have been unsuccessful due to it refusing to work unless the type is set to application/json. The setup includes an nginx server running node/express. I placed the manifest.json in the public folder, but the se ...

Node.js encounters issues when attempting to rename a folder

I am facing an issue while attempting to rename a folder within a WordPress theme after performing search-replace operations using a node script. The renaming process for the folder seems to be unsuccessful. My objective is to change this public_html/wp- ...

execute the node application using the .desktop file

Hello, I am attempting to launch an application in Linux by double-clicking it. I have come across the .desktop file as a solution (I need this method because the app will be deployed on a Raspberry Pi and users prefer not to use the terminal). Here is wha ...

How can I send a variety of different MongoDB queries to EJS?

After a user submits a form on my website, I aim to present them with a breakfast item, lunch item, and dinner item. Initially, I envisioned achieving this by individually performing db.collection("recipes").findOne queries, then passing the result to an E ...

Heroku is showing an Error R10 (Boot timeout) message, indicating that the web process was unable to bind to the designated $PORT within one minute of launching

Encountering an error while trying to deploy my first node project on Heroku. Here is the error message: 2020-09-29T04:24:09.365962+00:00 app[web.1]: production 2020-09-29T04:24:09.415266+00:00 app[web.1]: server is listening at port 40890 2020-09-29T04:24 ...

different spot to store .npm directory (so NFS-mounted home is not used)

tag, the following discussion is taking place: While attempting to install global packages via npm on my NFS-mounted home folder, I encountered an issue. Specifically, when I tried installing jshint using the command sudo npm install -g jshint, an error o ...

CSS animation for image hover effect in Revolution Slider

I'm currently facing an issue while using Revolution Slider. To better illustrate, I am referring to the original demo found at this link: . When browsing through the 'portfolio' tab, you'll notice that the images shrink and darken upo ...

Issues with testing incorporating jest, react, webpack, and SVG

I recently delved into the world of React development, but I've hit a snag when trying to run a test with an SVG file. You can find the code in my GitHub repository https://github.com/alejomongua/react-playground. Upon running npm run test, I encoun ...

Centering an icon in tandem with Typography text using Material UI

Is there a way to align an icon with the text so that they are on the same level? Currently, it seems like the icon is slightly above the text. I've tried using padding-top: 5px and margin-top: 5px, but those solutions didn't work as expected. ...

The CSS slider is stuck on the fifth slide and won't move past it

I encountered an issue with the CSS slider I am using, as it only had five radio buttons / slides in its demo. After attempting to add more slides, I noticed that the slider was not scrolling to the new slides. I'm unsure where I made a mistake in m ...

Heroku's Node Express experiencing unexpected spikes in service response time

My Node application on Heroku is experiencing sporadic spikes in service times, sometimes reaching over 20000ms when it should be around 50ms. These spikes occur during static file loads and a simple API call that provides a heartbeat connection to active ...

What is the best way to generate a table with continuously updating content?

If the user input : $sgl = 2; $dbl = 0; $twn = 1; $trp = 0; $quad = 0; $price_room = 250000; I want the result looks like the picture below : https://i.sstatic.net/iQYqr.jpg I have tried the following code : <?php $sgl = 2; $dbl = 0; ...

Primeng - Concealed dropdown values within a scrollable table header

Recently, I integrated Primeng p-table with a filter and frozen column feature (with one column being fixed while the others are movable). However, I encountered an issue with the select dropdown in the header. When I try to open the dropdown, the values a ...

NodeJS Q Promise throwing an error: "does not contain 'then' method"

I'm currently in the process of incorporating a promise (my first time utilizing them with node) but I'm encountering this error: TypeError: Object function (){ Users.findById(req.body.receiver).exec(function (err, user){ if(err) return ...

Dealing with Redis session management in the event of a database disconnection

Having trouble with losing connection to Redis, which is used for sessions in my Express App. var RedisStore = require('connect-redis')(express); sessionStore = new RedisStore(config.db.redis.connection); sessionStore.client.on('error' ...

Tips for validating a field by referencing data from a different model

I'm attempting to verify the validity of a date field by cross-referencing information stored in another model within the database. During API testing, the validation appears to work correctly as it triggers the exception, but the insertion actually ...

Using Jquery fadeIn along with responsive CSS media queries

Issue at Hand: An issue arises with my navigation bar and text block animations. The fading effect applied on the text element causes inline styles to be added, thus disrupting the media query set to hide the text on smaller screens. If you disable the s ...