2

Good day! I'm having a hard time fixing this issue. I'm currently using node js webserver (http).

I'm a beginner in using node js so any help would be appreciated.

What I'm hoping to achieve is to display a string 'Hello World!' in the browser while accessing it through the URL. The problem is I'm running the script from a remote server and unfortunately I can't access it through the URL.

The script is running fine but for the browser it returns an error saying:

host didn’t send any data.
ERR_EMPTY_RESPONSE

Here is the script I'm running from the remote server:

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write('Hello World!');
  response.end();
}).listen(2000);

I think my script doesn't have a problem. So I'm guessing it's from the setup of the server, but I don't have any idea in which part it's causing not to display it. I'm currently using a Linux Server.

Thanks in advance!

acknolodgia
  • 217
  • 1
  • 4
  • 16

2 Answers2

0

From what I can see you are listening on port 2000, are you sure that the url you are requesting the data from also contains the port e.g http://localhost:2000/ ?? Browsers by default tries to connect using port 80 on http and 443 for https, if you are listening on a different port than those, you have to define it in the url, by using a ":" after the domain/ip address

Anyway, have a look at the express module for server side rest APIs, will make request handling so much easier:

const express = require('express');
const app = express().listen(80);

app.get("/",function(request,response){
    response.send('Hello World');
});
Clayton Bez
  • 114
  • 3
0

Express allows you to handle the creation of web servers better. But node or express, do make sure that the URL you have entered consists of the port number that you have asked the server to listen to.

Another possibility is that the port you have asked to display your response is already being used by another server. You can try using a different port number.

You might have gotten the answer, but this is for the folks out there who are new to node at present and have stumbled upon this stackoverflow question! Good day :)

Akanksha
  • 121
  • 1
  • 2