http - 如何在没有端口范围和 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');
由于我不熟悉网络,我应该如何修改它。
解决方案
添加这行代码:
if (pathname == '/') pathname = '/index.html';
在这行代码之后:
var pathname = url.parse(request.url).pathname;
&在这行代码中更改8080
为:80
listen(8080, '0.0.0.0');
推荐阅读
- javascript - 下划线链接
- r - 使用 dplyr、自定义函数或 purr 的多个条件 if-else
- javascript - Angular - 意外的令牌 <
- arrays - “Int”类型的值没有成员“intValue”
- amazon-web-services - 在 AWS elasticsearch 中索引 pdf 文件
- javascript - 动态设置值以反应形式控件Angular
- javascript - 如何调试“未捕获的 RangeError:超出最大调用堆栈大小”?
- java - 如何通过jsoup从div中获取特定元素?
- javascript - ERROR 错误:未捕获(在承诺中),无法匹配任何路由。网址段
- javascript - 事件侦听器仅在 Javascript 和 HTML Canvas 中侦听屏幕上的两个数字