0

I'm learning how to use Node.js apps in a server (not my local machine). In this situation, I'm following this tutorial in order to create a simple web server.

I have installed Node.js, and tested via CLI that it works. I can run javascript code, generate output in the CLI, etc. But I can't manage to make the an app work via URL using a browser.

The app folder structure I have is very simple:

index.js
package.json
package-lock.json

This is the code I'm currently working with in index.js:

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

var server = http.createServer(function (req, res) {   // 2 - creating 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); //3 - listen for any incoming requests

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

If I go via CLI to the app's folder and run the app (node index.js), I can see the expected console.log() output (Node.js web server at port 5000 is running..). But if then I go to the URL pointing at that folder, which I would expect to output <html><body><p>This is home Page.</p></body></html>, it doesn't work (see below for what I get).

I have a domain (http://example.com) with document root in the app's folder. Here's what I have tried:

None of those give the response I would expect. I don't know if this is a problem with my file structure, my server configuration... but I've been looking around and I can't figure why this basic example isn't working in my case, clearly I'm missing something. Any help would be much appreciated!

EDIT: The server where I'm trying to run this app is a LAMP machine with CentOS 7.

11
  • “Jt Doesn’t Work”. Anything? 👥. Commented Feb 5, 2021 at 2:10
  • "I have a domain (example.com) with document root in the app's folder." - the domain doesn't really matter, apart from it pointing to your web server. But you need to tell us more about your server installation. What "document root" are you talking about? Is this is a server with software preconfigured to serve static files? That's not how you run node.js applications. Commented Feb 5, 2021 at 2:15
  • 2
    It works for me. If you are getting the source code of your index.js file then you are running another web server on port 80 but if you are getting the console.log line then then on port 5000 it should work. Commented Feb 5, 2021 at 2:17
  • 1
    This is also not using https so you should use http and the correct urls are example.com:5000/ not example.com/:5000 Commented Feb 5, 2021 at 2:18
  • Which Linux You Got? 👥. Commented Feb 5, 2021 at 2:20

1 Answer 1

2

I have checked in my server (Nginx), your code works fine.

After reading @MinusFour 's comment, I suspect that you didn't open 5000 port in your server's security groups.

Security Groups are like this picture

If that not work, you can also check your server's firewall setting.

As for your domain expression:

You can refer to https://support.google.com/gsa/answer/6329145?hl=en


I haven't got 50+ reputations to comment so I have to answer here.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the right answer! After I opened port 5000 (and used http instead of https) it works! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.