如何在默认端口上设置节点js应用程序?

我的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启动节点服务器(假设您的apache已启动),您将收到权限错误。 正确的做法是反向代理您的节点应用程序,并通过Apache提供服务。 你的apacheconfiguration应该如下所示。

  <VirtualHost *:80> ServerName localhost ServerAlias localhost DocumentRoot /path/to/your/node/app Options -Indexes ProxyRequests on ProxyPass / http://localhost:2121/ </VirtualHost> 

此外,我的build议是如果可能的话使用Nginx,使生活更容易…

默认情况下,http端口在80端口上运行,所以如果你这样做

`

 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('hello'); }).listen(80); console.log('Server running'); 

`

您将能够访问您的内容在http:// localhost /

还记得你不能在一个端口上运行多个应用程序。 不起作用。