JS Script using DOM running on pm2 server - javascript

I have read that serverside, you cant use DOM. I've created a web application using DOM and I wonder what needs to be done to reproduce this code to make it work on a server, so the people on the internet can work with it. Here is a little peak of a code:
// Listen for export button click
document.getElementById('exportForm').addEventListener('submit', function (e) {
setTimeout(exportData, 20);
e.preventDefault();
});
// Export data function
function exportData(e) {
console.log("Exporting...");
// device details and time range details
const devId = (document.getElementById('deviceInput')).value;
var dateFrom = (document.getElementById('dateFromInput')).value;
var dateTo = (document.getElementById('dateToInput')).value;
var timeFrom = (document.getElementById('timeFromInput')).value;
var timeTo = (document.getElementById('timeToInput')).value;
const limit = (document.getElementById('limitInput')).value;
const key = (document.getElementById('keysInput')).value;
When I try to run it on server using pm2 start app.js, it returns this error:
ReferenceError: document is not defined
at Object.<anonymous> (/home/cloud/sites/App/app.js:6:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
I've heard about JSDom, but I think there needs to be html included in a string and then I have no idea how to connect the css in there and so on. Is there a better way for me to make this work?

Document object is a browser feature hence you cant use it inside server, Instead you have other features like the FS (File System)
Anyhow using JSDom may be possible, you have to rewrite using inline styles https://www.codecademy.com/article/html-inline-styles which is not a good practice and you should avoid it

Related

Mutex timeout in javscript does'nt show on what line in code the error occurs

I use Node v16.14.2 and run this program in the command prompt.
I have a little problem with using Mutex in javascript. The Mutex itself do work but sometimes it takes to long to aquire the Mutex and I run into the error:
C:\myproject\node_modules\async-mutex\lib\errors.js:4
exports.E_TIMEOUT = new Error('timeout while waiting for mutex to become available');
^
Error: timeout while waiting for mutex to become available
at Object.<anonymous> (C:\myproject\node_modules\async-mutex\lib\errors.js:4:21)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\myproject\node_modules\async-mutex\lib\Semaphore.js:4:16)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
I have 2 things I wonder about:
The error message doesn't show on what line this error occurs in my code which is a problem because I have perheps 20 different mutexes and I don't know which one has the problem. How to know on what line in my code this error occurs?
(All other types of errors that is not related to the mutex shows which line the error occurs to mention)
Is there a problem to let the code aquire the mutex anyway even if it is not avaliable (which probably is not a good idéa but still?)
Test code to reproduce the error. I have nested the same mutex just to illustrate the error.
var Mutex = require('async-mutex').Mutex;
var withTimeout = require('async-mutex').withTimeout;
const mutexWithTimeout = withTimeout(new Mutex(), 5000); //5 second timeout
_testfunction();
async function _testfunction() {
console.log("starting...");
await mutexWithTimeout.runExclusive(async () => {
console.log("I am inside the mutex now...");
await mutexWithTimeout.runExclusive(async () => {
console.log("Timeout will happen here because I am nested just to reproduce the error");
});
});
}

TypeError: jQuery.getJSON is not a function [duplicate]

I'm new to Node.js. I already have a frontend javascript script that uses an API and gets weather data like so:
function getTextWeatherUsingStation(theStation){
theURL = 'https://api.weather.gov/stations/' + theStation + '/observations/current';
// return new Promise((resolve, reject) => {
$.getJSON(theURL,function(data){
var myJSON = JSON.stringify(data)
I read that you can't just use a library as is. I converted it over to a Node.js friendly file by wrapping it in
module.exports = {
and altering the function to:
getTextWeatherUsingStation: function(theStation){
It was having problem with promise so I just commented it out as you can see.
I was getting errors on the $.getJSON line and figured it was because I hadn't included jQuery so I used npm to install that.
I'm still getting this and can't figure out why:
....\drupal-8.4.5\stationFunctionsNode.js:34
$.getJSON(theURL,function(data){
^
ReferenceError: $ is not defined
at Object.getTextWeatherUsingStation (C:\Users\edwin.brown\Sites\devdesktop\drupal-8.4.5\stationFunctionsNode.js:34:13)
at Object.<anonymous> (C:\Users\edwin.brown\Sites\devdesktop\drupal-8.4.5\nodeTestWData.js:8:18)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
$.getJSON is jQuery's function for front to make http requests.
You are on backend now. So things will be little different here. This is not how you make requests from backend instead should consider using one of the native modules to make http requests which is http module and you use it like
http.request(Options, function(res) { ...
Or if you want to use something like getJson here is get-json library that you can use like
getJSON('URL', function(error, response){..
Ofcourse you'll have to install it first with npm install get-json
then use in your project like var getJSON = require('get-json')

Backticks in node.js

I am attempting to use the following code as a file test2.js with node.js version 12.0.
let apiKey = '117fa18eaf7312fa52f593c6d52fc48d';
let id = '6094817';
let xu1 = `http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey)`;
console.log('xul is: ', xul);
I get the following results and I am unable to understand why.
jgossage#jgossage-XPS-8700:/LinuxData/Projects/node-weather$ node test2.js
/LinuxData/Projects/node-weather/test2.js:4
console.log('value is: ', xul);
^
ReferenceError: xul is not defined
at Object.<anonymous> (/LinuxData/Projects/node-weather/test2.js:4:27)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11
The only thing I can see is that possibly the template string is not being handled properly, resulting in the variable xul being not defined.
As #Andreas mentioned you have a typo it should be
console.log('value is: ', xu1);
but also even if you run that you get
xu1 is: http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey)
That is because you have to use ${} instead of $().
So the final fix is
let xu1 = `http://api.openweathermap.org/data/2.5.weather?id=${id}&APPID=${apiKey}`;

generating spectrogram form wav file nodejs and save it

i'm new in the world of javascript and i wanted to recreate a project of mine (written in python) with javascript.
so i want to write a script with nodejs that reads a wav file and generate from it a spectrogram.
I used node-wav , canvas and audio-context trying to recreate the method applied in this module
I'm working with local files from my laptop and with javascript client side. I want to do everything locally. This is my code :
const canvas = createCanvas(480, 240)
const spectro = spectrogram(canvas, false)
const spectrogramsGenerator = async () => {
// wavfiles return a list of wav files (it works)
const files = await wavFiles()
for (file in files) {
const buffer = fs.readFileSync('path/to/file/' + files[file])
spectro.connectSource(buffer, audioContext)
spectro.start()
}
}
with that code snippet i get the following error :
/path/to/workspace/node_modules/spectrogram/spectrogram.js:34
window.onresize = function() {
^
ReferenceError: window is not defined
at new Spectrogram (/path/to/workspace/spectrogram/spectrogram.js:34:5)
at Spectrogram (/path/to/workspace/node_modules/spectrogram/spectrogram.js:16:14)
at Object.<anonymous> (/path/to/workspace/spectrogram.js:13:17)
at Module._compile (internal/modules/cjs/loader.js:736:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
at Function.Module._load (internal/modules/cjs/loader.js:560:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
at executeUserCode (internal/bootstrap/node.js:526:15)
if this the wrong way doing it (i think it is) .. what packages should i use to achieve my goal or how should i approach this problem. Thanks in advance

Unit testing web-browser independent JavaScript

I have client-side JavaScript that does not interact with the DOM or the web-browser in any way. I would like to unit-test the functionality of this code (which is just a fancy database/buffer) in Travis-CI without starting up a web-browser. Command-line JavaScript made me think I need node.js. I looked through various unit-testing libraries and decided on Mocha for its simplicity, however testing a browser-based module/class seems to be excessively difficult with a node.js based library.
Specifically, I want to test this (simplified) browser JavaScript valid code:
// I need this NameSpace to organise my code and isolate from other code
var Nengo = {};
Nengo.DataStore = function(dims) {
this.times = [];
this.data = [];
for (var i=0; i < dims; i++) {
this.data.push([]);
}
}
Nengo.DataStore.prototype.push = function(row) {
this.times.push(row[0]);
for(var i = 0; i < this.data.length; i++){
this.data[i].push(row[i+1]);
}
}
When I try to test in Node.js, I can't import the idea of the Nengo namespace properly. This test can't even run:
// get the datastore code from the folder below
require("../simple_data")
var assert = require("assert")
describe("DataStore", function() {
var data_store = new Nengo.DataStore(2);
it("accepts data", function() {
data_store.push([0.0, 1.1, 1.2])
assert.deepEqual(data_store.data, [[1.1], [1.2]])
});
});
It fails with the following error:
/home/saubin/javascript_test/test/simple_test.js:5
var data_store = new Nengo.DataStore(2);
^
ReferenceError: Nengo is not defined
at Suite.<anonymous> (/home/saubin/javascript_test/test/simple_test.js:5:22)
at context.describe.context.context (/usr/local/lib/node_modules/mocha/lib/interfaces/bdd.js:49:10)
at Object.<anonymous> (/home/saubin/javascript_test/test/simple_test.js:4:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:192:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:189:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:422:31)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:398:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
One way I have thought of solving this is abandoning node and outputting the test results to the DOM of a headless browser and getting the results, but this seems like a lot of excessive overhead. Can I change the structure of my code to be compatible with Node.js? Is there some other solution that I'm not seeing due to lack of knowledge in the area?
You can make your code "node.js-aware" so it puts global definitions into the actual global context while still remaining completely compatible with the browser environment:
if (typeof window !== "undefined") {
// in browser, define global to be an alias for window
// so global can be used to refer to the global namespace in
// both the browser and node.js
var global = window;
}
global.Nengo = {};
(function() {
var Nengo = global.Nengo;
Nengo.DataStore = function(dims) {
this.times = [];
this.data = [];
for (var i=0; i < dims; i++) {
this.data.push([]);
}
}
Nengo.DataStore.prototype.push = function(row) {
this.times.push(row[0]);
this.data.push(row.slice(1));
}
})();
Then, just remember that ANY global definitions, must be explicitly assigned to the global namespace. In node.js, this will assign them to the actual global namespace in node.js. In the browser, this will assign them to the window object which is the global namespace in the browser.
Although, #jfrien00's answer is technically correct, I came up with another answer later.
First, I needed to refactor my JavaScript code to declare my namespace outside of the DataStore file. Then I declare the Nengo global variable as jfriend00 describes global.Nengo = {};, but only in my Mocha test file.
This way, when I run in my web browser and my unit tests, my code is tested as expected.

Categories

Resources