【问题标题】:How to access the web page without port range and index.html如何在没有端口范围和 index.html 的情况下访问网页
【发布时间】:2020-11-07 11:56:33
【问题描述】:

我的网站地址是http://www.aboutsamyuen.com:8080/index.html

这是我在第一页显示的内容:

但是,如何在没有端口范围和http://www.aboutsamyuen.com 的情况下访问页面

仅,这意味着当我在浏览器中输入此链接时,它将显示 Hello World。

我正在使用 aws 服务,这是我在 server.js 中的代码:

var http = require('http');
var fs = require('fs');
var url = require('url');

http.createServer( function (request, response) {
  var pathname = url.parse(request.url).pathname;
  console.log("Trying to find '" + pathname.substr(1) + "'...");

  fs.readFile(pathname.substr(1), function (err, data) {
    if (err) {
      response.writeHead(404, {'Content-Type': 'text/html'});
      response.write("ERROR: Cannot find '" + pathname.substr(1) + "'.");
      console.log("ERROR: Cannot find '" + pathname.substr(1) + "'.");
    } else {
      console.log("Found '" + pathname.substr(1) + "'.");
      response.writeHead(200, {'Content-Type': 'text/html'});
      response.write(data.toString());
    }
    response.end();
  });
}).listen(8080, '0.0.0.0');

由于我对网络不熟悉,应该如何修改。

【问题讨论】:

    标签: http web networking server


    【解决方案1】:

    如果我使用 listen(80, '0.0.0.0'); ,这是错误消息

    【讨论】:

    • 正如错误截图右上角所说,Cloud9 不允许您在除 8080 之外的任何端口上运行您的应用程序。您可以在本地系统上的端口 80 上运行您的代码并通过http://localhost/ 访问该站点(不包括端口)。
    • 但是,我想在 Cloud9 上运行我的应用程序。我该如何解决?
    【解决方案2】:

    添加这行代码:

    if (pathname == '/') pathname = '/index.html';
    

    在这行代码之后:

    var pathname = url.parse(request.url).pathname;
    

    &在这行代码中将8080改为80

    listen(8080, '0.0.0.0');
    

    【讨论】:

    • 谢谢,按照您的指示,我可以使用http://www.aboutsamyuen.com:8080 访问该页面,但是如果我在listen(8080, '0.0.0.0') 中将8080 更改为80,则会出现错误并且代码无法运行。
    猜你喜欢
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多