61

I'm new to Node.js and wish to run a program using streams. With other programs, I had to start a server simultaneously (mongodb, redis, etc) but I have no idea if I'm supposed to run one with this. Please let me know where I am going wrong and how I can rectify this.

This is the program:

var http = require('http'),
feed = 'http://isaacs.iriscouch.com/registry/_changes?feed=continuous';


function decide(cb) {
setTimeout(function () {
if (Date.now()%2) { return console.log('rejected'); }        
cb();
}, 2000);
}

http.get(feed, function (res) {

decide(res.pipe.bind(res, process.stdout));


//using anonymous function instead of bind:
// decide(function () {
//   res.pipe(process.stdout)
// });

});

This is the cmd output:

<b>C:\05-Employing Streams\05-Employing Streams\23-Playing with pipes>node npm_stre
am_piper.js

events.js:72
throw er; // Unhandled 'error' event
          ^
Error: Parse Error
at Socket.socketOnData (http.js:1583:20)
at TCP.onread (net.js:527:27)
</b>
starball
  • 20,030
  • 7
  • 43
  • 238
user3514893
  • 611
  • 1
  • 5
  • 3
  • Are you sure the URL http://isaacs.iriscouch.com/registry/_changes?feed=continuous is right? I get nothing from there, but I do get something from http://isaacs.iriscouch.com/registry/changes?feed=continuous (without the underscore). Also see [here](http://nodejs.org/api/http.html#http_http_get_options_callback) for an example of how to catch errors from http.get. – user3374348 Apr 09 '14 at 12:35

6 Answers6

90

Close nodejs app running in another shell. Restart the terminal and run the program again.


Another server might be also using the same port that you have used for nodejs. Kill the process that is using nodejs port and run the app.

To find the PID of the application that is using port:8000

$ fuser 8000/tcp
8000/tcp:            16708

Here PID is 16708 Now kill the process using the kill [PID] command

$ kill 16708
Ganesh Pandey
  • 5,216
  • 1
  • 33
  • 39
  • 3
    This error can also be thrown if your Node app is expecting to connect to a port which is not available, even if it's not being used by another process. For example, my app was expecting to connect to a port on a Vagrant box I set up, but I had not started that box. – sheldonkreger Jan 21 '15 at 22:16
  • @sheldonkreger, could you please elaborate on this issue with opening a port in vagrant? because i think that i am having the same issue (i get this error when trying to run a meteorjs app inside a vagrant box). thanks! – Sorin Postelnicu Jun 26 '15 at 06:40
  • The issue was that my Vagrant instance was not running at all. That's why the port was unavailable! :-) – sheldonkreger Jun 27 '15 at 16:35
  • If the port number is occupied, then most probably you will see such an error message. – derek Oct 26 '15 at 06:47
  • i tried what you said but $ fuser 80/tcp returned nothing, can you suggest me where i am going wrong. – samar taj Shaikh Sep 20 '17 at 18:04
  • @samartajShaikh it's because nothing is running on port 80. – Ganesh Pandey Sep 21 '17 at 00:52
15

I had the same problem. I closed terminal and restarted node. This worked for me.

Umopepisdn
  • 807
  • 6
  • 16
Vu Luu
  • 792
  • 5
  • 16
  • This solved this for me, Because, I was running this using port-30, and was trying to run it again on same port in other terminals . lol yes this solved it . closing the terminals :P – Seetpal singh Jul 20 '16 at 13:31
3

Well, your script throws an error and you just need to catch it (and/or prevent it from happening). I had the same error, for me it was an already used port (EADDRINUSE).

kungfooman
  • 4,473
  • 1
  • 44
  • 33
2

I always do the following whenever I get such error:

// remove node_modules/
rm -rf node_modules/
// install node_modules/ again
npm install // or, yarn

and then start the project

npm start //or, yarn start

It works fine after re-installing node_modules. But I don't know if it's good practice.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

Check your terminal it happen only when you have your application running on another terminal..

The port is already listening..

Codedreamer
  • 1,552
  • 15
  • 13
-3

For what is worth, I got this error doing a clean install of nodejs and npm packages of my current linux-distribution I've installed meteor using

npm install metor

And got the above referenced error. After wasting some time, I found out I should have used meteor's way to update itself:

meteor update

This command output, among others, the message that meteor was severely outdated (over 2 years) and that it was going to install itself using:

curl https://install.meteor.com/ | sh

Which was probably the command I should have run in the first place.

So the solution might be to upgrade/update whatever nodejs package(js) you're using.

Gerbrand
  • 1,561
  • 1
  • 12
  • 20