Does node's exec call block meteor application? - node.js

I am building a meteor application that creates pdf files from a web page using phantomjs.
PhantomJS is called with node's child_process.exec function within a fiber using Meteor._wrapAsync. When I am using any url like google.com, it is working fine and the pdf is created.
The problem is, I want to capture a site served by the same meteor application at /invoicePDF. But during the execution of phantomjs the site apparently does not load, thus, it cannot load inside phantomjs, which makes finishing the call to phantomjs impossible.

The solution seemed to be to use Meteor.bindEnvironment instead of Meteor._wrapAsync. Unfortunately, I have know idea why.

Related

Can I run A-Frame in node.js (server-side)?

I'd like to take a lot of screenshots and export .glb from thousands of generated a-frame models. Since I can't save files from the browser without prompt, generating the models on the server-side with node.js would be awesome.
(As a workaround I could probably generate the models in the browser and POST them to a local server that saves the files to disk but I'd prefer the server-side rendering if possible)
So, how can I render A-Frame models in node.js?
Tried approaches like this with mock-browser for mocking the DOM (which is necessary in three, let alone DOM-based a-frame) and headless-gl for creating a WebGL context in node.
No luck there, so I settled with an electron application.
tldr: check this out. Download, npm install; npm run start; and render screenshots / export the scene as glb. If you like it, let me know how to improve it to make it more useful.
non tldr:
You can load up an a-frame website using electron, but use the main process to save the screenshot on your drive - so there are no prompts, and it can be automated.
To make a render, You can simply use the native screenshot component to create a canvas containing the screenshot image
document.querySelector('a-scene').components.screenshot.getCanvas('equirectangular');
but instead of downloading the blob, pass it into the main process via an renderer to main IPC channel - where you can save it on your drive.

general question about reactJS, nodeJS and NPM

first, the "facts" that I know:
reactJS is frontend framework, after compiling it'll generate a bundle of html/js/css files that can be recognized by modern browsers, so the browser straightforward request the web host server for these static html files ;
nodeJS is a server-side environment, or sometimes people call it a backend framework, browsers don't understand nodeJS but have to request the web host server to interpret it and to send back the html response.
according to wiki, npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js.
my question is:
is my understanding correct
why reactJS also using npm and includes node_module, does it have something to do with nodeJS
Javascript is the programming language used in both Node.js and in the browser, but that's about as far as the similarities goes. Node uses the V8 engine, which is the same running in the chrome browser.
Even though they use the same engine, they are quite different. They both have plenty of APIs - most notably, the browser has the DOM (which allows you to render stuff to the screen). In Node.js you do not have the DOM, but there are other APIs, for accessing the filesystem for instance.
Since both are running JavaScript, you can try to run the same program both in node and in the browser, but it will sometimes fail. React is a front-end framework which uses the DOM, so if you try to run React in node, it will crash because in Node there is no DOM (Node has no screen to render to). If you try to run a program suited for Node.js in the browser, it will crash if it uses some Node-specific API.
The confusing bit is that npm is a package manager for JavaScript, not necessarily node (even though node is in the name..). Therefore, we can use npm also for web applications. (If we do, we need to use a bundler like webpack though).
Some npm modules might work both in the browser and in node, but some will only work on one of them. If the npm module requires some platform-spcific API (DOM for the browser for example) then it will not work for the other platform.
Hope that clears things out! I understand that it's a bit of a mess..

Do I need to use WebSockets to interface node.js with PhantomJS?

I couldn't find it referenced in the documentation but since PhantomJs is now at 2.1, Is there a way to use a WebSocket to interface NodeJs and PhantomJs directly or am I better going with StdIn/StdOut?
I'd like to skip the step of spawning a webserver plus a fake webpage to catch the events like I've seen on many github. If I can I would avoid those ninja tricks.
You really don't need WebSockets or the Web Server module.
There are basically two ways to use PhantomJS from node.js:
Write a plain PhantomJS script and spawn a PhantomJS process that executes that script, but this doesn't provide easy interactivity
Interface with PhantomJS through the many available bridges so that you can mix PhantomJS code with node.js code:
phantom, node-phantom, nightmare (version <1.8 used PhantomJS, now it's Electron), Horseman, navit, phridge, node-phantom-simple, jquery.go.js, SpookyJS (wrapper around CasperJS, which is a wrapper around PhantomJS), Selenium-Webdriver (JavaScript bindings), webdriver.io (other JS bindings for Selenium), etc.

Cannot find module 'nw.gui' - node webkit updater

I'm trying to use node webkit updater for my aplication,but when i'm trying to test is(using npm test) or start it(using nmp start) i'm receiving this error .Both commands are used from Nodejs command prompt .
The js file used is updaterClientABC.js and the error comes from this line: var gui = require('nw.gui');
This is my folder structure :
Can you please tell me what should i do ? I admit that i am a beginner i've never worked with node-webkit.Some advices will be very helpful :)
I noticed this thread:
node module 'nw.gui' not found
So yes, this kind of thing is due to how NW.js gets called (directly or from within a project). I've run into this problem as well. In this case, in the above, you call updaterClientABS.js directly from node.js. To run a node-webkit project you need to load it via the NW.js binary, which in turn requires node.js.
It's also possible, as was my problem with this, that I was trying to access the nw.gui within the Node context (vs browser context). My solution was to access it via the browser context (which has more global access, not just Node.js objects).

Run node.js app with node-webkit?

I have a nodejs app with modules, views etc..
Is it possible to open this app with node-webkit instead of opening it in the browser ?
Thanks.
Yes, it is. Node-WebKit is a package with browser + node web server. But I believe you will probably want to change your architecture, because you don't need the client-server style. If you run your app without any changes, node-WebKit will act just like node server and you still need a browser to access the app. There is no reason to use node-WebKit instead of pure node in this case.
To use node-WebKit embedded browser you should know that there are no need to start node server. The browser's JavaScript environment is already connected to node and you can execute node commands and packages direct from the JavaScript files (eg.: access file system from the browser, a dream to every web developer). It's like you are running a browser inside the server, without the need of make requests and receive responses... For this reason you don't need to use packages as socket.io, cause the communication is already established. But you can use the fact of node is a server to easily establish communication between different machines, for example.

Resources