【发布时间】:2020-11-07 02:24:45
【问题描述】:
我使用 aws cloud9 作为后端,想通过自己的公共 IP 访问 index.html。但是,出现错误,无法成功运行。这里有什么问题?
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, '54.25x.xxx.xxx'); // Or 8081 or 8082 instead of 8080. Or '127.0.0.1' instead of 'localhost'.
如果我输入 .listen(8080, 'localhost'),代码可以运行并创建服务器 但是,如果我将其更改为 IPv4 公共 IP,则无法运行。
错误如下:
Debugger listening on ws://127.0.0.1:15454/*********
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL: address not available 54.25*.***.***:8080
at Server.setupListenHandle [as _listen2] (net.js:1263:19)
at listenInCluster (net.js:1328:12)
at doListen (net.js:1461:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1307:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Waiting for the debugger to disconnect...
【问题讨论】:
标签: node.js linux amazon-web-services http server