简体   繁体   English

Node.js-将HTML提供给远程客户端

[英]Node.js - Serve HTML to a remote client

I am serving an HTML file from my Node.js server. 我正在从Node.js服务器提供HTML文件。 The server code is - 服务器代码是-

var port = 3000;
var serverUrl = "0.0.0.0";

var http = require("http");
var path = require("path"); 
var fs = require("fs");         
console.log("Starting web server at " + serverUrl + ":" + port);

var server = http.createServer(function(req, res) {

    var filename = req.url || "/realtime-graph-meterNW.html";
    var ext = path.extname(filename);
    var localPath = __dirname;
    var validExtensions = {
        , '.css'   : 'text/css'
        , '.html'  : 'text/html'
        , '.js'    : 'application/javascript'
    };
    var isValidExt = validExtensions[ext];

    if (isValidExt) {

        localPath += filename;
        path.exists(localPath, function(exists) {
            if(exists) {
                console.log("Serving file: " + localPath);
                getFile(localPath, res, ext);
            } else {
                console.log("File not found: " + localPath);
                res.writeHead(404);
                res.end();
            }
        });

    } else {
        console.log("Invalid file extension detected: " + ext)
    }

}).listen(port, serverUrl);

function getFile(localPath, res, mimeType) {
    fs.readFile(localPath, function(err, contents) {
        if(!err) {
            res.setHeader("Content-Length", contents.length);
            res.setHeader("Content-Type", mimeType);
            res.statusCode = 200;
            res.end(contents);
        } else {
            res.writeHead(500);
            res.end();
        }
    });
}

When i try to run the HTML file from a browser on my own PC, using http://localhost:3000/realtime-graph-meterNW.html , it works fine. 当我尝试使用http://localhost:3000/realtime-graph-meterNW.html从自己的PC上的浏览器运行HTML文件时,它工作正常。

But when I try to access the same from a browser on another PC on the same network using my IP address, I get an error - Failed to load resource: the server responded with a status of 503 (Service Unavailable) . 但是,当我尝试使用IP地址从同一网络上另一台PC上的浏览器访问同一浏览器时,出现一个错误- Failed to load resource: the server responded with a status of 503 (Service Unavailable)

I don't get where I'm going wrong with this. 我不明白我要怎么做。 Any suggestions ? 有什么建议么 ?

Probably your local firewall is blocking connections to your machine for port 3000. 可能是您的本地防火墙阻止了端口3000与计算机的连接。

Check your firewall settings. 检查您的防火墙设置。 You must allow your PC in the firewall settings to be accessible from the outside. 您必须允许从外部访问防火墙设置中的PC。

Also check this question and answers: 还要检查此问题和答案:

How could others, on a local network, access my NodeJS app while it's running on my machine? 本地网络上的其他人如何在我的机器上运行我的NodeJS应用程序时访问它?

Like the accepted answer says on the question I linked, you have to make sure that you put the correct URL, like for example http://192.168.3.200:3000 (if your IP is 192.168.3.200), when trying to access your server from the LAN. 就像接受的答案在我链接的问题上说的那样,在尝试访问您的主机时,必须确保输入正确的URL,例如http://192.168.3.200:3000 (如果IP为192.168.3.200)。局域网中的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM