2016-06-13 55 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的,讓生活輕鬆了許多......

相關問題