1

I started learning nodejs and stopped at the lesson where the server is created, here is the code for this script:

var http = require('http'); // Import Node.js core module

var server = http.createServer(function (req, res) {   //create web server
    if (req.url == '/') { //check the URL of the current request
        
        // set response header
        res.writeHead(200, { 'Content-Type': 'text/html' }); 
        
        // set response content    
        res.write('<html><body><p>This is home Page.</p></body></html>');
        res.end();
    
    }
    else if (req.url == "/student") {
        
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><p>This is student Page.</p></body></html>');
        res.end();
    
    }
    else if (req.url == "/admin") {
        
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><p>This is admin Page.</p></body></html>');
        res.end();
    
    }
    else
        res.end('Invalid Request!');

});

server.listen(5000); //6 - listen for any incoming requests

console.log('Node.js web server at port 5000 is running..')

I experiment on a remote machine (google cloud virtual machine), run the script using node (I see a message in the console that the server is running) but if I go to the IP address through the browser (for example http://92.233.12.12:5000/) I don't I see the result, what am I doing wrong? did not find any additional information, everywhere in the lessons access through localhost:5000/ ...

bazylevnik0
  • 60
  • 1
  • 7

2 Answers2

0
res.write('<html><body><p>This is admin Page.</p></body></html>');
res.end();

You can also do the following:

res.end('<html><body><p>This is admin Page.</p></body></html>');
0

Please check if you are able to telnet the port 5000 for the remote server IP from your laptop terminal. It could be that a firewall is blocking the port for the outside access. If you want to check this on the server side you can login to the google machine and from the terminal of the server try curl http://localhost:5000 and if you see the response , then it should be the firewall.

Seshu Ss
  • 35
  • 4
  • 1
    TELNET says: "Connecting to IP.IP.IP.IP ... Failed to open a connection to this node, on port 5000: Connection failed". added permission on the server to port 5000 in the firewall, but still the same error – bazylevnik0 Sep 09 '20 at 17:14
  • Did you also check from the remote server terminal using the command curl http://localhost:5000/ ? Can you post the result of the command here. – Seshu Ss Sep 09 '20 at 17:24
  • when requesting via "curl localhost:5000" from the server, the response comes with the content of the page - that is, the correct one: "

    This is home Page.

    "
    – bazylevnik0 Sep 09 '20 at 17:29
  • So it makes clear that the issue is because you are not able to reach the GCP server on port 5000 using the IP. This could be due to mostly the firewall issue . You have to check the GCP documentation to allow the port 5000 on the IP you are using and this solve your issue. Or if you want to test via browser , install chrome or firefox on the GCP server and use xterm to open the browser on the remote server (if the server is linux) and it should work from that browser using http://localhost:5000/ – Seshu Ss Sep 09 '20 at 17:35