javascript - 如何在默认端口上设置node js应用程序?

标签 javascript node.js server

我的机器上也运行着 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/

相关文章:

mysql - MariaDB + Galera 节点 RAM 使用率 99%

python - 如何在 Linux 服务器上指向 Python 的本地副本?

javascript - 如何一次只显示一个文本框并单击下一步显示下一个输入字段

node.js - 使用模拟对使用请求、管道和流的私有(private)方法进行单元测试

javascript - HTML 表单提交禁用并更改点击文本

node.js - 锁定作业在议程 Node.js 中如何工作?

android - Cordova 插件媒体 : Parse ".amr" Audio File on nodejs server

c - 为什么我的服务器只将文件的第一行写入客户端?

javascript - 修改合金项目中的小部件

javascript - 使用CSS从div悬停的左侧推送背景颜色