我想将我的计算机作为公共服务器。我尝试使用NodeJS下面的代码。而不是将主机名命名为127.0.0.1,而是使用我的真实IP地址。但它不起作用。是什么原因?是否可以以这种方式创建公共服务器?或者还有其他替代方法吗?
const http = require('http');
const hostname = '175.157.210.25';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server2 running at http://${hostname}:${port}/`);
});
输出
C:\Users\Nuwanst\Documents\NodeJS\test2>node app.js
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL 175.157.210.25:3000
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at Server._listen2 (net.js:1245:19)
at listen (net.js:1294:10)
at net.js:1404:9
at _combinedTickCallback (internal/process/next_tick.js:83:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
C:\Users\Nuwanst\Documents\NodeJS\test2>node app.js
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL 175.157.210.25:3000
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at Server._listen2 (net.js:1245:19)
at listen (net.js:1294:10)
at net.js:1404:9
at _combinedTickCallback (internal/process/next_tick.js:83:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)