How to obtain location of the code being executed in Nodejs - javascript

Trying to create a logger javascript class, that I add as a require() in other javascript files. One of the functions of this logger should be, that it writes to the console which script is currently running.
They way I thought it would execute , was that I have a path.basename(__Filename) function inside my logger, but how would I go around targeting the filename of the script executing the logger script?

There is no way to know which file the script itself is in as the browser loads all the scripts under the DOM while loading the page.
function xyz(){
console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}
This should give you the name of the function that's being called as long as the function is declared in function xyz(){} format. In this case xyz.

Related

javascript implementing problem in creating a ms teams app

I am new at creating Microsoft Apps and i managed to build an app (group tab) and install it in Teams.
How can i attach an external javascript file to the Tab.js?
import './MyFunction.js'; //get's ignored
How can i generally use javascript code in the Tab.js , because it wont even accept the script tag without returning an error?
Compile with Script - ErrorMessage
(The only thing i found out was that you need the javascript client sdk, but i cannot find examples or how/where to you use it in the Tab Script)
To attach one javascript file in another javascript in your example you need to export Myfunction in Myfunction.js and import Myfunction from './Myfunction.js' in Tab.js. For second question you cannot use script tag in Tab.js, in return() function you need to write html code and in render() function you need write functions you want to call.

Initialization PostgreSQL with Node Js

I'm trying to delete some entries based on the creation/expiry date in a PostrgreSQL DB only at the beginning, when the node server starts.
At present I put the line
DELETE FROM ....db WHERE date <= CURRENT_DATE
in the main route and it works, but that also mean that each time I refresh the page Node gets to execute that line, over and over again.
Is there a way to add a functionality for which the database knows and executes that line only once at the very beginning?
Should I create a different javascript function that fires at the beginning of the server.js file?
I'm using PostgreSQL, NodeJs and Express (no sequelize or similar).
Thanks.
Should I create a different javascript function that fires at the beginning of the server.js file?
Yes.
JavaScript is a scripting language, which means that the code gets executed in the outer block (global scope). There is no main() function as in C/C++. So you should just put this line after the database initialization code in the outer program scope.

How to access webpack created js functions in html

I have an express server that compiles handlebar files into html based on the route and I am using webpack to compile javascript files into one js file per page the user navigates to.
I am trying to access global functions that I defined in my javascript files from my html files to register onclick events and I can't seem to get it to work. The page loads fine
Here is the resulting HTML from the handlebars file:
<button onclick="OnButton_Open()">
Here is the function in the webpack compiled javascript:
OnButton_Open = function () {
here's my function
}
If I leave the function like above, the page loads, but when I click I get an error that it can't find OnButon_Open.
window.OnButton_Open = function () {
here's my function with the global prefix
}
If I prefix the function with window. which is what I thought that I had to do in this situation, I will actually get a reference error as the page loads that OnButon_Open is not defined. This is what the resulting webpack code looks like at the spot where I get the error:
/* harmony export (immutable) */ __webpack_exports__["OnButton_Open"] = OnButton_Open;
Is there a better way to make those functions accessible to the outside world? I'd happily put everything in the global scope for now until I can modularize this app a bit more.

Get path of lazy loaded JS file from within the file?

If I'm loading a script using something like
document.getElementsByTagName("head")[0].appendChild(scriptElement);
…what code can I put inside of that loaded JS file to get the URL of itself?
In other words: I use dom injection to load http://foo.com/foo.js. From within foo.js, how do I get the URL http://foo.com/foo.js?
If you have included the scriptElement object in your dom, then you should know the "scriptElement.src" - so inside foo.js you should know the source:
alert(scriptElement.src);
I've found a solution that works for me here:
javascript - get node reference to script tag that calls a function
The answer that requires you to throw an error in the loaded file, catch it, and then pass it to the global function in the loading page did the trick. (It doesn't work in IE, but for my current project that is not a concern.)

Loading javascript files as and when needed

I am working on a widget/gadget based website, as in the user can choose which widgets/gadgets s/he wants on the screen.
This means that I will have hundreds/thousands of js files, 1 per widget/gadget. I thought that I could use $.getScript to load the correct js file as and when needed based on user selection.
The problem I have discovered is that $.getScript can only be sent variables via javascript global variables. Global variables and $.getScript don't play too well together it seems.
Is there another way of doing this? i.e. somehow only load js files as and when needed based on user selection where the loaded js file can receive variables from the js file which calls the sub js file?
The solution for this problem is to use a module loader system like require.js.
It will automatically know which files to load, based on each files named dependencies.
Your modules will clearly outline their depenedncies like this:
require(["helper/util"], function(util) {
//This function is called when scripts/helper/util.js is loaded.
//If util.js calls define(), then this function is not fired until
//util's dependencies have loaded, and the util argument will hold
//the module value for "helper/util".
});
Once they've done that the require.js loader will load up the correct JS files automatically for you. It's bit of a different syntax, but it will solve the problem.
The problem I have discovered is that $.getScript can only be sent variables via javascript global variables. Global variables and $.getScript don't play too well together it seems.
Scripts loaded via $.getScript can access globals just fine. (Not that global variables are a good idea.)
But another way to pass information to your loaded script would be for each script to implement a post-load callback based on the script name, and for your loader code to call that callback (with the arguments you want to pass to it) once $.getScript does its thing.
Live Example | Source
Of course, that example uses globals too — $ for jQuery, and the new script's theNewScript load complete function is a global. In a production system, to do this, I'd have just one global for my entire app (say, MyApp), define all of my code within a scoping function to avoid creating any other globals at all, and have the new script set its "load complete" function as a property on that. (Normally I don't define any globals at all, because I don't normally demand-load scripts.)
try to call your scripts using pure javascript
var html_doc = document.getElementsByTagName('head')[0];
js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', '../scripts/jquery-1.7.2.min.js');
html_doc.appendChild(js);
js.onreadystatechange = function () {
if (js.readyState == 'loaded' || js.readyState == 'complete') {
doSomething
}
}
//works on ff e no chrome
js.onload = function () { doSomething }

Categories