【问题标题】:how to set node js app on default port?如何在默认端口上设置节点 js 应用程序?
【发布时间】:2016-10-14 01:25:56
【问题描述】:

我的机器上也运行着 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(末尾没有端口号。)

【问题讨论】:

  • 您只需执行.listen(80) - 但是,如果 apache 已经在端口 80 上运行,它将无法工作,因为您不能让两台服务器在同一个端口上侦听。
  • 可能是时候了解更多关于 Apache mod-proxy 插件以及如何将流量从 :80 重定向到 :2121
  • 好问题好友@vikrant。

标签: javascript node.js server


【解决方案1】:

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,让生活更轻松......

【讨论】:

    【解决方案2】:

    默认情况下,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/访问您的内容

    还请记住,您不能在一个端口上运行多个应用程序。它不会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 2016-02-19
      • 2019-12-03
      • 2013-09-03
      • 1970-01-01
      • 2012-01-18
      • 2019-02-24
      • 2022-01-10
      相关资源
      最近更新 更多