0

I am very new to node.js and I am wondering the best way to test the following type of code in general. Right now I am doing it all as one file in a netbeans node.js project. I am getting no output, the output should be "HELLO SERVER".

it usually compiles with no errors, but there is no output, other times it says

"throw er; // Unhandled 'error' event Error: listen EADDRINUSE :::8000"

I already know what this rejection means and I think it is just doing that when I click run twice because then the port is occupied by the first run.

I have already tried on multiple ports, it works but with no output...

Should I be testing this some other way? Why is there no output? Output should be "HELLO SERVER" Thank you.

var http = require("http");
http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    request.on("data", function (chunk) {
        response.write(chunk.toString().toUpperCase());
    });
    request.on("end", function () {
        response.end();
    });
}).listen(8000);

var http = require("http");
var request = http.request({
    hostname: "localhost",
    port: 8000,
    method: "POST"
}, function (response) {
    response.on("data", function (chunk) {
        process.stdout.write(chunk.toString());
    });
});
request.end("Hello Server");
0

2 Answers 2

0

This means that your port 8000 is already in use by another process. That process is probably an older instance of your server still running (not properly exited) that's still taking the port.

Try looking for the process using port 8000 and kill it

On Linux

fuser -k 8000/tcp

On Windows

netstat -ano | findstr :8000
// Find the value of the PID (last column to the right)
taskkill /PID {pid_value} /F
Sign up to request clarification or add additional context in comments.

Comments

0

I tested your code and the server starts up perfectly fine. The "throw er; // Unhandled 'error' event Error: listen EADDRINUSE :::8000" means that the port you're starting the server on already has a server or another process running on it. Change the port number or stop the service

var http = require("http");
http.createServer(function(request, response) {
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });
  request.on("data", function(chunk) {
    response.write(chunk.toString().toUpperCase());
  });
  request.on("end", function() {
    response.end();
  });
}).listen(3000);

var http = require("http");
var request = http.request({
  hostname: "localhost",
  port: 3000,
  method: "POST"
}, function(response) {
  response.on("data", function(chunk) {
    process.stdout.write(chunk.toString());
  });
});
request.end("Hello Server");

11 Comments

On the code you posted it is listening on a different port than it is requesting from, is that correct? I already looked up what the rejection means, sometimes it is running without that rejection but just with no output showing as it is doing now. I also switched the port to 0 which makes it look for any open port and it displays no output text....This question isn't just asking what that rejection means, it is asking the best way to test something like this in general and why is there no output...
@evanr123 thanks, missed that. And if that's your question you should edit it or re ask it to specify that you understand the error and want to know how to test it. Try maybe restarting your machine or clearing all ports from the terminal and see if you still get the error.
I will try restarting. So when you ran it you got the output to be "HELLO SERVER" ? or did it just start up with no output....
Yes, I got the output HELLO SERVER and everything worked as expected. Your code is fine the port is just in use by another process.
Just want to say that if you're still getting an issue after restart and youre trying ports like 8001, 8002 and other clearly unused ports than it is probably not an issue with node or your code. Maybe a networking issue? Ports are in use by your router?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.