2016-06-13 56 views
1

我有我的机器上运行的Apache,我必须运行我的应用程序,而无需添加端口号。如何在默认端口上设置节点js应用程序?

当我用下面从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如何

+0

的可能的复制[Node.js的 - 我如何从URL删除端口?] (http://stackoverflow.com/questions/9526500/node-js-how-can-i-remove-the-port-from-the-url) –

+0

你只是'.listen(80)' - 但是,如果apache在端口80上运行已经无法工作,因为您不能让两台服务器在同一个端口上侦听。 – MrWillihog

+0

可能是时候了解更多关于** Apache mod-proxy **的附加组件,以及如何将流量从80重新定向到:2121 –

回答

0

默认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/

访问您的内容也请记住,你不能在一个端口上运行多个应用程序。不起作用。

2

Apache正在为您占用80端口。如果您尝试使用端口80启动节点服务器(假设您的apache已启动),您将收到权限错误。正确的做法是反向代理您的节点应用程序并通过apache提供服务。你的apache配置应该如下所示。

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

另外,我提醒你的是,如果可以使用Nginx的,让生活轻松了许多......

相关问题