Executing NodeJS custom middleware to show parent function being called

Goal: Showcase the parent function of a middleware function

shared              = require('./RoutFuctions');

app.post('/link', shared.verifyToken, (req, res) => {
...
}

In the middleware function

exports.verifyToken = function(req, res, next) {
   console.log('this function was triggered by ' + ?parentFunction? )
   ...
   next()
}

Is there a way to replace ?parentFunction? with something more specific than __filename? OR is it possible to pass an optional parameter instead?

Answer №1

Resolved this issue by including a parameter that points to the previous node on the client side.

  return axios
    .post('/link', {
      token: token,
      caller: 'myParentFunction'
    })
...

then within the verifyToken function, the caller variable can be accessed using req.body.caller

exports.verifyToken = function(req, res, next) {
    console.log('verifyToken caller = ' + req.body.caller)
   ...
   next()
}

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

How can we optimize axios requests with lodash debounce?

Utilizing a state prop named network busy status to control elements in the UI. Due to the rapid changes in status, my spinner appears overly active. Is there a simple method, utilizing lodash _.debounce, to throttle this section of code? const instance ...

Using node.js to extract images from a PDF document

I am looking for a way to utilize PDF similar to how ZIP or RAR is used. My goal is to store numerous images (traditional Tibetan Buddhist literature), around 60000 ideally. However, dividing them into 10-100 volumes is also acceptable. Any tool can be ut ...

Matching a regular expression pattern at the beginning of a line for grouping strings

One of my tasks involves working with a markdown string that looks like this: var str = " # Title here Some body of text ## A subtitle ##There may be no space after the title hashtags Another body of text with a Twitter #hashtag in it"; My goal ...

What is the best way to distinguish between npm packages that are designated as peer dependencies?

I am currently facing a challenge in removing unused packages from the package.json files of multiple projects due to peer dependencies causing issues. While tools like depcheck attempt to identify all "unused" packages, they do not distinguish between tru ...

Incorporating an HTML image into a div or table using jQuery

I am a beginner in using JQuery within Visual Studio 2013. My question is how to insert an img tag into a table or div using JQuery? For example, I have a div and I would like to generate an image dynamically using JQuery. Or, I have a dynamically create ...

Implementing a strategy to prevent the browser's back button from functioning

Is there a way to prevent the user from using the back button in a single page application? I've tried methods like onhashchange and window.history.forward, but they don't seem to be effective (perhaps because the URL doesn't change). ...

"Unlocking the Potential: Maximizing the Benefits of the top.gg Vote Web

My bot has been verified on top.gg, and I'm looking to offer rewards to users who vote for my bot. How can I detect when someone votes for my bot, get their ID, check if it's the weekend, and take action after the vote? Essentially, how do I util ...

Establishing a connection with MSSQL 2014 through Node.js

I've been grappling with this issue for quite some time and I just can't seem to figure it out. Here is the code snippet that I have been using: const sql = require('mssql/msnodesqlv8'); const pool = new sql.ConnectionPool({ server: ...

How do I ensure my object is fully constructed before sending it in the response using NodeJS Express?

Currently, I am in the process of constructing a result_arr made up of location objects to be sent as a response. However, my dilemma lies in figuring out how to send the response only after the entire array has been fully constructed. As it stands, the re ...

Querying user locations using Mongoose's geospatial capabilities

Currently delving into the world of nodeJS, utilizing express along with mongoDB and mongoose for ORM functionalities. The task at hand involves creating a User entity and persisting it in the database, while also retrieving the user's location data a ...

What is the best way to utilize Gulp and Browserify for my JavaScript application?

Looking to modernize my app with a new build system and I could use some guidance. It seems like I need to shift my approach in a few ways. Here's the current structure of my app: /src /components /Base /App.jsx /Pages.jsx /. ...

Searching for the "MongoDB" object using Node.js to execute an array query

[ { "_id" : ObjectId("5abce64d86d3289de052a639"), "username" : "doggy", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4e454d4d536a594b484b4449435f44435c044f4e5f">[email protecte ...

Ways to refresh UI in ReactJS without triggering a specific event

In my React application, I am displaying various pictures and GIFs that users can attach to a post. Currently, each image has an onClick handler which triggers either a modal with different options or deletes the picture if the user holds down the ctrl key ...

Creating a multi-dimensional array using information from a database

I have a unique challenge where I am utilizing a template that generates a menu with a specific structure. In my database, I have various menus stored and I've successfully retrieved them. However, the issue arises when I need to figure out a way to d ...

What is the best way to display a Nested JSON structure without an object key?

Need help with extracting data from two different JSON structures. The first one is straightforward, but the second is nested in multiple arrays. How can I access the content? See below for the code snippets: // First JSON { "allSuSa": [ { ...

Error message: "No schema found for model 'User'.Create a schema using mongoose.model(name, schema)" with identifier 'MissingSchemaError'

I have been working on developing a schema for a user authentication system but keep encountering the error message mentioned above. I recently created two new pages with the code provided below: Users.js var mongoose = require ('mongoose'); va ...

Error encountered while using XLSX.write in angular.js: n.t.match function is not recognized

I've encountered an issue while trying to generate an .xlsx file using the XLSX library. The error message I received is as follows: TypeError: n.t.match is not a function at Ps (xlsx.full.min.js:14) at Jd (xlsx.full.min.js:18) at Sv (xlsx.full.min ...

The Step-by-Step Guide to Deselecting an Angular Checkbox with a Button

I currently have a situation where I have three checkboxes labeled as parent 1, parent 2, and parent 3. Initially, when the page loads, parent 1 and parent 3 are checked by default, while parent 2 is unchecked. However, when I manually check parent 2 and c ...

Make sure to add the local private NPM dependency before running the prepublish script

Within my application package.json file, I am referencing a local private NPM dependency like this: "core-module": "file:///Users/myuser/Documents/projects/core_module" My goal is to have the local private dependencies (such as core-module) automatically ...

Updating a behavior object array in Angular 5 by appending data to the end

After creating a service to share data across my entire application, I'm wondering if it's possible to append new data to an array within the userDataSource. Here is how the service looks: user.service userDataSource = BehaviorSubject<Array& ...