All,
Trying to use node.js to isolate JS plugins created by others through the child_process API in node.js. I can't get the child process to interact with the parent, I get no errors running this code but the client can't send or receive messages, or write to console.log, so I assume the JS isn't running. I do get a console message that the debugger is running from both the parent as well as the client. Code is pretty much straight out of the node.js API docs so I'm not sure why this won't work. Perhaps something wrong with the Windows environment?
parent code:
var fork = require('child_process').fork;
console.log('Starting plugin server...');
var myChild = fork('client.js');
myChild.on('message', function (msg) {
console.log('PARENT got message:', msg)
});
myChild.send({ hello: 'world' });
client.js:
console.log("child");
process.on('message', function (msg) {
console.log('CHILD got message:', msg)
process.send({msg: 'Message receivd & resent from the child.'})
});
process.send({msg: 'Message from the child.'});
Output:
debugger listening on port 5858
Starting plugin server...
debugger listening on port 5858
I'm using Windows 8.1, visual studio 2013 pro with the new tools for node.js add-ins.
Any pointers appreciated. I'm new to node.js so possibly making a silly mistake??
I had problem with fork() on windows machines. Solution/workaround was with silent mode and logging. Here is link https://github.com/rogerwang/node-webkit/issues/1516
Related
On my server, every time a user uses our service we have to grab a JSON file for them from the server. I do this by using fs.createReadStream() inside of my own function.
function getJSONFromServer(filepath, callback){
var data = fs.createReadStream(filepath);
data.on('error', function (error) {
console.log("Caught", error);
callback(undefined, error);
});
var jsonFile = "";
data.on('data', function(chunk) {
jsonFile += chunk;
});
data.on('end', function() {
var jsonData = JSON.parse(jsonFile);
callback(jsonData);
data.destroy();
data.close();
});
}
This does the job, but it does not close the connection to the file. So after reading 1024 files (the limit on my server), Node.js will then produce the error EMFILE, too many open files. Then I have to kill our Node.js server, open it again and that will clear the "open files".
I check the amount of files open by lsof -i -n -P | grep nodejs. It displays something like this:
nodejs 13707 node 10u IPv4 1163695 0t0 TCP 127.0.0.1:55643->127.0.0.1:27017 (ESTABLISHED)
nodejs 13707 node 11u IPv4 1163697 0t0 TCP 127.0.0.1:55644->127.0.0.1:27017 (ESTABLISHED)
for as many files that are open.
I've tried using graceful-fs. I've tried calling stream.destroy() and stream.close(), but I still get the same issue. My server is essentially a ticking time bomb because we get a heavy, steady flow of users and after so many users have connected it will just stop working.
Also, ulimit -n [open file amount] does not work, and even if it did, this is not a long term solution because I'd like my file connections to close and not sit open for no reason.
I'm using Node.js version v0.10.25, Ubuntu 15.04 (GNU/Linux 3.19.0-42-generic x86_64) and the latest version of graceful-fs if that helps!
Thanks for any help you can provide.
This has got to be the stupidest mistake I've ever made. Regardless, here's the answer. I hope I can save someone from dealing with this error and almost ripping their hair out.
I was running my app with nodejs and not node. Turns out, if you do nodejs --version, it will likely return a version that is very old, which was v0.10.25 for me. node --version however was v5.6.0. Obviously this massive jump in versions would fix some stuff, so I ran the app with node app.js instead of nodejs app.js and I haven't had the issue at all since. There are now only 6 open files, whereas before we had over 1000 with time.
Damn it feels good to have this off my chest.
I'm having an issue where reloading (ctrl + c and then running again with node app) a basic Node.js server (tried v0.12.0 and v0.12.7) on Windows 10 causes the server to hang for about 20 seconds before reloading. When using a tool like PM2 to watch my server, it errors out after any update to the file and the log shows an EADDRINUSE error.
I boiled this down to the most simple possible example (thinking it might be an issue with Express) but it's still happening with the basic example from the Node.js documentation:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3030);
console.log('Server running at http://127.0.0.1:3030/');
I've checked that nothing else is running on port 3030. I've also tried different port numbers. It just seems that the Node.js process is not shutting down and releasing the port in a timely manner. I've also tried using ctrl+break, and also capturing the SIGINT and manually calling process.exit(). I can confirm that it catches the signal, but it still hangs as described.
I first noticed this shortly after upgrading to Windows 10 from 7, and was not having issues before, so I would think this is the most likely culprit. I have a dual-boot with Ubuntu 14.04, and I am having no issues there with the same code.
I'm no longer having the problem I described: it was likely something very specific to my setup since it wasn't reported anywhere else. I have since updated to Node 4.2.1 and installed various Windows updates, so hard to say what the original cause was.
I'm using pm2 (https://github.com/Unitech/pm2) in my node.js project. Also I'm sending logs of errors of the app in Logentries (https://logentries.com).
I wonder is it possible to log uncaught exceptions from the app (when something fails badly and pm2 restarts the app for example)? I know that using process.on('uncaughtException') is bad practice so would like to hear some suggestions.
Thanks!
Where did you read that process.on('uncaughtException') is a bad practice?
As long as you exit the process after logging the exception I don't see what's bad, here is an example:
process.on('uncaughtException', function(e) {
console.error('Ouch, an unhandled exception');
//I like using new Error() for my errors (1)
console.error(e instanceof Error ? e.message : e);
process.exit(1);
});
(1): Javascript Error reference
Edit
pm2-interface is now deprecated, use require('pm2') instead. You will be able to do exactly the same as below by using bus system events.
An alternative with pm2 is to use pm2-interface and listening to the process:exit or process:exception events:
var ipm2 = require('pm2-interface')();
ipm2.on('ready', function() {
console.log('Connected to pm2');
ipm2.bus.on('process:exception', function(data){
console.log(data.pm2_env.name + 'had an exception');
});
});
This is really usefull when managing more than one process through a monitoring process.
You might want to check the blog post on how to build a custom pm2 logger. It can give you some ideas about monitoring processes through pm2-interface.
I am using Cloud 9 IDE to develop a simple CRUD application using Sails.js (node.js MVC framework). Up until today I had no trouble starting the Sails.js server.
Today, I've been trying to start the sails js server, but I keep getting this error:
warn: error raised: Error: listen EACCES
error: Server doesn't seem to be starting.
error: Perhaps something else is already running on port 8080?
I have checked my /config/local.js file and everything is just fine, as it should be. The port is set to process.env.PORT || 1337 so it shouldn't have any problems firing the server up.
I'm looking forward to your insight.
Thank you!
Open terminal and run this command:
$ lsof -i :8080
Output will show PID of process occupying port 8080: "httpd 1234 ....'
Then kill the process with this command
$ kill -9 1234
Sails will now run
Hmm-- looks like port 8080 isn't available. What happens if you try to switch the port? You may have another server running on that port. Or in some cases, hosts require the hostname to be set. I'd try switching the port first though.
The only real answer to this is: wait. C9 seems to kill servers in a weird way that causes Sails to jack up and blocks you from establishing another server. lsof -i doesn't show anything serving... but it still won't start. Seems to be an issue with Cloud 9 and Sails.js. If I serve a generic Node.js "Hello World" app on the same port, the issue doesn't occur. However, time, it seems, cures all. After awhile, Sails seems to snap out of it and starts serving again when lifted.
Incredibly weird.
All I want to do is deploy my little nodeJS app onto the free hosting site, appFog.
Nomatter what ports I set on my client side or on my server side.. I consistently get the error message:
events.js:71
throw arguments[1]; // Unhandled 'error' event
^ Error: listen EADDRINUSE
When this is on my laptop / desktop running on localhost, everything works just fine.
So this is what I've got going on:
Client side:
this.connection = new WebSocket('ws://super1onate.aws.af.cm:1337');
Server Side:
var express = require("express"); // load the express module
var app = express(); // App now holds the server object
// What ports to listen on
app.listen(process.env.VCAP_APP_PORT ||1337);
server.listen(process.env.VCAP_APP_PORT || 1337, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort); });
Your server code looks ok. What is events.js? It looks like maybe you're including a module that's trying to bind to a port it shouldn't.
Once you get your server running, I don't think your client code will work. As far as I can tell, AppFog doesn't support websockets, and if it does, you'll probably want to hit port 80, not 1337.
Alright, I'm going to answer my own questions.
AppFog does not support WebSockets. websockets =/= socket.io btw fyi
Anyways, according to this site:
http://feedback.appfog.com/forums/171983-appfog/suggestions/3543100-add-websocket-support-to-node-js