Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

javascript - Why is Node.js simple http server app not working on server?

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.

question from:https://stackoverflow.com/questions/66056601/why-is-node-js-simple-http-server-app-not-working-on-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...