node.js - Node js-没有端口号的应用程序

标签 node.js apache url port

我刚开始使用 Node js,我在服务器上开发了一个应用程序和主机,但它仅在我分配 端口号 时运行,我希望它仅通过域名运行。 我搜索了很多,但它对我来说用处不大 我试图重写服务器中的httpd.conf文件

/etc/httpd/conf/httpd.conf

<VirtualHost 119.19.52.203:80>
ServerName aip.xyz
ProxyRequests off
<Proxy 119.19.52.203:80>
        Order allow,deny
        Allow from all
</Proxy>

ProxyPass / http://example.xyz:5000/
ProxyPassReverse / http://example.xyz:5000/
ProxyPreserveHost on

并点击此链接

  1. nodejs-Domain without port Number
  2. apache Server Configuration

这是我的httpd.conf文件代码(在服务器上自动生成)

<VirtualHost 118.18.52.203:80>
ServerName example.xyz
ServerAlias www.example.xyz
DocumentRoot /home/example/public_html
ServerAdmin webmaster@example.xyz
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/example.xyz combined
CustomLog /usr/local/apache/domlogs/aip.xyz-bytes_log "%{%s}t %I .\n%{%s}t %O ."
## User example # Needed for Cpanel::ApacheConf
UserDir enabled aip
<IfModule mod_suphp.c>
    suPHP_UserGroup aip aip
</IfModule>
<IfModule !mod_disable_suexec.c>
    <IfModule !mod_ruid2.c>
        SuexecUserGroup aip aip
    </IfModule>
</IfModule>
<IfModule mod_ruid2.c>
    RMode config
    RUidGid aip aip
</IfModule>
<IfModule itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID example example
</IfModule>
ScriptAlias /cgi-bin/ /home/aip/public_html/cgi-bin/
# To customize this VirtualHost use an include file at the following location
# Include "/usr/local/apache/conf/userdata/std/2_2/aip/aip.xyz/*.conf"

After That I have Insert this Code Just Blow Down of the Configuration

Listen 80
<VirtualHost example.xyz>
ServerName example.xyz
ProxyPass / http://example.xyz:5000/
ProxyPassReverse / http://example.xyz:5000/
ProxyPreserveHost On 
</VirtualHost>

This is My Routes Codes // app.js

var express = require('express');
port = process.env.PORT || 5000
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var soft1 = require('./routes/soft1');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('hjs', require('hogan-express'));
app.set('view engine', 'hjs');
app.set('layout', 'layout/default');
app.set('partials', {
mainHead: "include/main/head",
mainContent:  "include/main/maincontent",
mainSlider:  "include/main/slider",
mainLogo:  "include/main/logo",
mainTopmenu:  "include/main/topmenu",
mainSocial: "include/main/socialicons",
mainPortfolio: "include/main/portfolio",
mainSubmenu: "include/main/sbumenu"
});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/soft1', soft1);
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: err
});
});
}
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
    message:err.message,
    error: {}
});
});
app.listen(port, function() {
console.log('Listening on port ' + port)
})
module.exports = app;

它仍然无法正常工作并且无法正确路由。当我访问 example.xyz 时,它会显示目录结构(应用程序的所有文件和文件夹)。

最佳答案

这里有 2 个进程:

  • NodeJS 进程,为您的应用程序提供服务
  • httpd (Apache) 进程,通常用作在一个域上为多个应用程序提供服务的入口点。

他们都需要监听一个端 Eloquent 能访问。

通常的做法是让 Apache 或 Nginx 进程监听默认端口 80,这是唯一向外部连接开放的 http 端口。 然后使用此 Apache 进程代理您在其他端口(如 5000,不应为外部连接打开)上运行的应用程序。

在你的 httpd conf 中,你可以在你的域上有一个入口点,在默认端口 (80) 上,并使用反向代理在端口 5000 上访问你的 Node 应用程序:

Listen 80
<VirtualHost aip.xyz>
  ServerName aip.xyz
  ProxyPass / http://aip.xyz:5000/
  ProxyPassReverse / http://aip.xyz:5000/
  ProxyPreserveHost On
</VirtualHost>

当您想在 1 个端口(通常是 80)后托管多个应用程序时,这很有用,即:

Listen 80
<VirtualHost aip.xyz>
  ServerName aip.xyz
  ProxyPass /app1/ http://aip.xyz:5000/
  ProxyPassReverse /app1/ http://aip.xyz:5000/
  ProxyPass /appX/ http://aip.xyz:500X/
  ProxyPassReverse /appX/ http://aip.xyz:500X/
  ProxyPreserveHost On
</VirtualHost>

关于node.js - Node js-没有端口号的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32352889/

相关文章:

javascript - 如何在 webpack hook 中打破循环

php - 当启用 SELinux 时,如何允许 PHP 在 exec() 调用中使用 at 命令?

apache - 使用 htaccess 从 http 重定向到 https,某些子目录除外

java - 在 JSP/Servlet 中安全地传递参数(无框架)

php - 将 curl 命令转换为 php curl

javascript - 使用 JS 创建 url 实例

mysql - 如何在nodejs中使用hooks/sequelize更新数据库

javascript - 使用 Visual Studio Code 在 Node.JS 中定义类型

node.js - 类型错误 : Cannot read property 'user' of undefined

java - Procrun 与 org.apache.commons.daemon.Daemon