how to access the node api from a remote page in electron? - node.js

In my electron app , I load the remote page(index.html) from remote url like("http://xxxx/index.html"), then I am trying to send a ipc event to the main process then handle it , and failed, but if I put the the index.html in local fs and it is OK.
So my question is how to enable the page to access the node api (like require,ipc, etc.) from the remote page.
------ (main process)
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadUrl('http://localhost:8080');
//mainWindow.loadUrl('file://' + __dirname + '/index.html');
var ipc = require('ipc');
ipc.on('spawn-ext-process', function () {
console.log("spawn-ext-process");
});
--------- http://localhost:8080/index.html (render process)
<script>
var ipc = require('ipc');
ipc.send('spawn-ext-process');
</script>

AFIAK the ipc api doesn't use http. It essentially is sending a signal from one part of the program running on your local machine to another part of the same program on that machine.
If you want to send a signal over http you might want to use something like socket.io or websockets.

How to enable the page to access the node api (like require,ipc, etc.) from the remote page?
Depends on how you're loading the page. If you're using loadUrl on a BrowserWindow instance, node integration is enabled by default (you can disable it via the nodeIntegration option, see the docs).
If you're using a <webview> tag, then it's disabled by default and you can enable it via the nodeintegration attribute. See the docs. Note that with webview you can preload scripts that do have access to node and electron APIs safely, and then access to those objects are destroyed when the script has finished executing (see here).
Like #FelipeBrahm said, I would not give a remote page access to node.js or electron APIs unless it's your own page, and even if you do that, be cautious.

Related

How can you force #google-cloud/datastore to use 'http' when you query your datastore emulator

I have a node + vuejs app from which I'm trying to query my local db (created using datastore emulator).
My code is something like this
const { Datastore } = require('#google-cloud/datastore')
const datastore = new Datastore({
apiEndpoint: "http://localhost:<port>",
projectId: <my_project_name>
})
I then try to run one of the sample queries on Google's documentation
My output shows there is a successful connection but then I get the following error in the console
POST https://localhost:<port>/$rpc/google.datastore.v1.Datastore/RunQuery net::ERR_SSL_PROTOCOL_ERROR
From the error, I see that it has changed http://localhost:<port> to https://localhost:<port> i.e it is forcing a protocol of https for my local host which then obviously fails.
Is there something/option that I have to specify to ensure that it uses http when I'm making local calls or using the datastore emulator?
I found the solution and am posting it in case someone else has the issue.
I started going through the code itself and discovered a reference to this link. The page says
.....By default, the client library will use gRPC, which is a binary tranport based on HTTP/2. It's Node.js implementation, #grpc/grpc-js, uses Node.js http2 module.
If you need to use the client library in non-Node.js environment or when gRPC cannot be used for any reason, you can use the HTTP/1 fallback mode. In this mode, a special browser-compatible transport implementation is used instead of gRPC transport.
In browser context (if the window object is defined) the fallback mode is enabled automatically; set options.fallback to false if you need to override this behavior....
When I added fallback:false to the options, everything worked i.e. it no longer redirected to https and it connected to my data emulator.
I thought about it further and I believe this behavior is because my code was written in the renderer process for a Vuejs App which means there's a windows object. To confirm, I moved the code to the main process and did not include the fallback option and it worked.

electron with nodejs and angular

I am trying to set up Angular 5 project in Electron and also run nodejs in server side, i know that electron permit to create desktop app, but is it possible to create server side app with electron, also i'm not sure if it's possible to have the nodejs app and angular all in the same project with ngx electron.
If I understand you correctly, you want a desktop application that in some way also communicates with a server.
Sure, that's possible since Electron will bundle node.js in the generated binary.
The easiest way I can think of is setting up an HTTP API (e.G REST) on your server side (possible with any language, I'd recommend either PHP or Node.js) and use http in your ng/electron app to talk to the server.
http.get(options, (res) => {
// Do stuff
}).on('socket', (socket) => {
socket.emit('agentRemove');
});

How to modify the node js request timeout in core set up or node js default library?

In node js, Socket Timeout by default has been set for Two Minutes in the package itself which i found in the documentation. I want the location where this code is lying inside the package so that i can modify them.
I found the following alternate solution but I want it to be changed in the core library.
var serverInstance = http.createServer(app).listen(port, function(){
// code
}
serverInstance.timeout = 0; // for disabling the timeout
serverInstance.on('timeout', function(socket){
// custom code
});
For your Reference:
1.) Express.js is the server API
2.) connect.js is the session management API
The socket timeout is set by calling socket.setTimeout. When a socket is assigned to a server, the server triggers a 'socket' event.
So, you should be able to find out where the libraries you're using override the socket timeout. If they don't expose this as an option, you could override Socket.prototype.setTimeout to ensure that only the timeout you want is set.

Troubleshooting socket.io error message

I inherited a project which utilizes node.js and socket.io, both of which I am not too familiar with.
I installed node.js and npm via homebrew and socket.io via npm.
The current setup has two different "sites": The frontend, that the user interacts with, and the backend that handles all the communications with a DB. The application uses several different displays, and the previous developer deemed it best to use the current setup.
I can successfully start the node server by invoking node node-server.js. In the head of the node-server.js, socket.io is being called like:
var sys = require('sys')
var exec = require('child_process').exec;
var io = require('socket.io').listen(8090);
[...]
In my front end, I call socket.io like this:
<script src="http://192.168.1.111:8090/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.111:8090');
[...]
However, when I debug the page, I get the following error messages:
SyntaxError: Unexpected identifier 'to' (anonymous function) socket.io.js:1
ReferenceError: Can't find variable: io sample.html
In the terminal window, where I started the node server, I get the output:
debug - served static content /socket.io.js
I should add that the two sites reside in separate folders - I access the frontend through a local Apache server, the piece that interacts with the DB sits someplace else on the same machine.
How would I correctly call the socket.io in my client?
Is there a way to enable better debugging?
Update
So the to in the error message, is because whenever the browser tries to load the socket.js file, it gets the reply of Welcome to socket.io

How can I access to my nodeJS app in console on my server?

I have an application nodeJS started like a deamon with forever(https://github.com/nodejitsu/forever).
So now, how can I access to my app and be abble to watch and change values of variables with SSH connexion on my server?
How open console on my app without interuption of service?
thx.
You should give node-inspector a look.
There's nothing built-in for that.
However, you can create a function on the server that will eval whatever string you put as parameter.
To query that function, you can use ajax or socket.io for example.
Socket.io example
socket.on('evalStr', function (str) { eval(str);}); //server
socket.emit('evalStr', "myVar = 10;"); //client via console
Edit: Of course, you can add security such as testing a password before evaluating.

Resources