我的机器上也运行着 Apache,我必须在不添加端口号的情况下运行我的应用程序。
当我从 http://localhost:2121 访问它时它起作用了使用以下内容:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello');
}).listen(2121);
console.log('Server running');
如何将其设置为使用 http://localhost (末尾不带端口号。)
最佳答案
Apache 正在为您占用 80 端口。如果您尝试使用端口 80 启动 Node 服务器(假设您的 apache 已启动),您将收到权限错误。正确的方法是反向代理您的 Node 应用程序并通过 apache 提供服务。您的 apache 配置应如下所示。
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot /path/to/your/node/app
Options -Indexes
ProxyRequests on
ProxyPass / http://localhost:2121/
</VirtualHost>
另外,我给你的建议是如果可能的话使用 Nginx,让生活变得更轻松......
关于javascript - 如何在默认端口上设置node js应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37792931/