首页 > 解决方案 > 如何在没有端口范围和 index.html 的情况下访问网页

问题描述

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

这是我在第一页显示的内容:在此处输入图像描述

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

only,这意味着当我在浏览器中输入此链接时,它将显示 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');

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

标签: httpwebnetworkingserver

解决方案


添加这行代码:

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

在这行代码之后:

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

&在这行代码中更改8080为:80

listen(8080, '0.0.0.0');

推荐阅读