In my nwjs app, I load a _launch.js file from an HTML file:
<html>
<body>
<script type="text/javascript" src="_launch.js"></script>
</body>
</html>
And in my _launch.js file, I fire up the the Node processes I need for an express server and socketIO.
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
gui = require('nw.gui'),
__curDir = process.cwd(),
//keep the logic for the IO connections separate
ioServer = require(__curDir + '/server.js');
//configure Express to default web requests to /workspace/ folder
app.use(express.static(__curDir + '/workspace'));
ioServer.init(io, console);
server.listen(3000, function () {
console.log('HTTP server listening on *:3000');
window.location = 'http://localhost:3000/MyApp/';
});
The app launches just fine, and my express/socketIO connections are all perfectly working.
But while the console.log() in the server.listen() callback appears in my terminal, any messages I try to log from the server.js file (required earlier) never show up anywhere.
Any ideas why?
Per the nwjs wiki, any files loaded via require() should be running in the Node context (and mine otherwise appears to be) -- but for whatever reason, I cannot use console.log() to view logged information.
You have to run DevTools for Background Page by right clicking on your app and choosing the Inspect background page option from the context menu.
click for full size image
Here is a related bug report: 0.13-beta3 console.log doesn't work in node context
Just pass --enable-logging=stderr in chromium-args in your package.json:
{
...
"chromium-args": "--enable-logging=stderr",
...
}
Or use the Inspect background page DevTools as #Jakub Bejnarowicz has pointed out.
Please look at the list of changes related to node in the nw.js wiki.
Since node-webkit supports GUI applications instead of console applications, the output of console.log() (and other similar methods such as console.warn() and console.error()) is redirected to WebKit's console. You may see it in your “Developer Tools” window (on its “Console” tab).
Related
So I encountered the maybe strangest thing in the last years. It took me hours to make it reproducible but still, I can not explain it at all.
I have an application that loads variable stuff in an iFrame. Then host and iFrame communicate via postMessage. The application is in use for years and there were never such problems in production or development as the one we encountered when we wanted to write e2e-tests in Testcafe. In the tests, the communication between host and client fails under certain circumstances: The iFrame sends a message with some data but the data attribute of the postMessage is undefined at the host's end. WTF.
Under what circumstances? It seems that, when certain language constructs are used in the JavaScript of the page inside the iFrame, the message gets distorted. Again: only when using Testcafe. Regardless of with Chrome, Chromium, or Firefox.
Ho to Reproduce:
Have a test.html:
<!DOCTYPE HTML>
<html>
<body>
<h1 id='output'>test</h1>
<iframe srcdoc='<!DOCTYPE html><html><body>inner<script>setTimeout(() => {window.parent.postMessage({type: `communication worked`}, `*`);}, 1500);const x = new class { y = new class {} } </script></body></html>' sandbox="allow-forms allow-scripts allow-same-origin"></iframe>
<script>
window.addEventListener('message', (event) => {
console.log({event});
document.getElementById('output').innerText = event.data.type;
});
</script>
</body>
</html>
Serve it with your favorite server. I use node with a server.js int his example, ng in the real-life application.
const connect = require('connect');
const serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic(__dirname))
app.listen('8000', () => console.log('Server running on 8000...'));
And then, run the testcafe-test:
npx testcafe firefox e2e/src/test.ts
fixture`Foo`
.page`http://localhost:8080/test.html`;
test('Bar', async t => {
await t.wait(120000);
});
As you can find in the browser console the host is receiving the message from the client, but its data is empty.
Now, remove those characters y = new class {} from the srcdoc. Do the test again. Communication works, seriously WTF.
Maybe using new class inside of new class might be a strange style. If it would even be bogus somehow, how come there is no error at all? And how can it distort the postMessage without even being connected to it in any way? How does this problem only occur with Testcafe, not when you run the code by yourself or even with Protractor? I am maximum clueless.
Some Details:
In the real life the iFrame's content can contain also a more complex app, written in angular, with which the same thing happens. I am not able to say if it's the very same language construct in this case which "breaks the postMessage-system of the whole page" or a different one.
It doesn't change if the content of the src doc is not hardcoded (naturally it's not in the real-life application)
I use testcafe 1.18.2, Firefox 91.5.0, Chromium 90.0, Google Chrome, Debian 10
I am very thankful for the slightest hint!
I have installed a script tag in my shopify store. It is supposed to run when I load my store, when I look in my network panel, I can see that it was loaded but it failed. when I look under the console, it generated an error
net::ERR_CONNECTION_REFUSED. why is my store refusing to run my code?
post data looks like this
var postdata = {
"script_tag":{
"event":"onload",
"display_scope":"online_store",
"src":"https://localhost:3000/testscript.js"
}
}
There are a variety of reasons why your script tag could not be working. A few solutions are listed below:
1) Your URI should begin with "http://" (without the "s") seeing as this is a localhost server.
2) Try unchecking proxy server for your lan 127.0.0.1 through internet properties (how to locate this depends on your platform).
3) Port 3000 could already be in use, so try using something else like the widely-known 8080 or 1337. See here for more information on killing a Port 300 in use.
I fairly new to nodejs and I put the following code in some module,
module.exports = function (app) {
app.get('/aa', function(req, res) {
console.log("test");
debugger;
res.send('file updated');
...
the application is working and I see in the browser "file updated"
but I've two problems:
why don't I see "test" in the console?
I set up a debugger (using WebStorm 10) and it does not stop, what am I missing here?
I try also to run the debugger in Webstorm and click on debug and I see in the terminal Debugger listening on port 55236 but when I change the port in the browser nothing happen, any idea?
Node runs server side, so any console.log statements will appear in the the console you ran your script from. You won't see any debug statements in the browser except those from client js code. The reason you see file updated is because you sent that string as a response to your browser, and its simply spitting back the response it got.
Same thing with the debugger. If you are using your browser's debugger, it will only affect client js code, NOT your server code. Not too familiar with WebStorm, but they have instructions for debugging Node.js specific code here
I have a Debian 6 server that runs several domains.
I have created a new domain (that functions perfectly as a website), but I want to create a node.js scripts that listens on that domain.
Doesn't have to be a specific port, it can be the entire domain as far as I am concerned, but for simplicity I am trying to test on port3000
The directory structure is
domainname/public/(static content)
I am running the node at the domainname/ level.. i.e one level below the webcontent. I have a test index.html file in the public folder (which works fine). The test script is
var connect = require('connect');
var app = connect()
.use(connect.static('public'))
.use(function (req, res) {
res.end("Couldn't find it.");
})
.listen(3000);
the script runs but going to
http://domainname:3000
just throws me a page cannot be found error.
Any advice would be gratefully appreciated. Either a script change, or a different module maybe
Ultimately, there was too much going on with regards to the server which was already a full functional website.
I ended up creating an EC2 micro instance on AWS, installing NODE afresh, opening up the required port and uploading my code and BOOM worked first time.
Thanks for all those taking time to offer an answer
I have an app running on http://localhost:6543 - it's a Pyramid app.
This app serves the AngularJS app at /
This app uses socket.io itself
The question is: is it possible to test that application using those tools ?
I have this in my scenario.js file:
beforeEach(function() {
browser().navigateTo('http://localhost:6543/');
});
but the moment I launch testacular (with run or start), I get this error message:
Chrome 23.0 registration: should delete all cookies when user clicks on "remove all" button FAILED
browser navigate to 'http://localhost:6543/'
/home/abourget/myapp/jstests/scenarios/registration_scenario.js:9:5: Sandbox Error: Application document not accessible.
so I understand the browser doesn't give access to the iframe's document, because it'd be some Cross-Origin violation.
What I tried:
Proxying to my app using the Testacular web server (with the proxies option), but / would conflict with Testacular's own serving of its framework. Also, both apps would eventually try to use /socket.io and that would conflict also.
Doing the reverse (tweaking my app to proxy to Testacular's server), but then, we'd get the same issues with /socket.io.
Thanks for these great tools, btw!
Instead of having
beforeEach(function() {
browser().navigateTo('http://localhost:6543/');
});
change this to
beforeEach(function() {
browser().navigateTo('/');
});
and then in your testacular-e2e.conf.js file add:
proxies = {
'/': 'http://localhost:6543/'
};
You might still have other issues, but I can reproduce the "Sandbox Error: Application document not accessible." message with just the Pyramid Hello World App and this configuration problem.
We had a similar problem, and had already proxies and navigateTo('/'). We needed to add some urlRoot to avoid conflicts when loading socket.io. We simply added '/e2e' and that was enough to solve the conflict. Actually, there was a warning message when running testacular for this issue.