Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js- Application Without port Number

I am just started working with node js and I had developed an application and host on the server but its runs only when I assign the port number I wanted its runs through only with domain name. I search a lots, buts its not use full for me I tried to rewrite the httpd.conf File in the server

/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

and also follow this link

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

This is My httpd.conf File Code (automatic Generated On the Server)

<VirtualHost 118.18.52.203:80>
ServerName example.xyz
ServerAlias www.example.xyz
DocumentRoot /home/example/public_html
ServerAdmin [email protected]
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;

Its Still Not Working and not Routes properly. When I access the example.xyz its show directory structure (all files and Folder of the application).

like image 544
Akhilesh Singh Avatar asked Sep 02 '15 12:09

Akhilesh Singh


1 Answers

You have 2 processes here :

  • NodeJS process, which serves your application
  • httpd (Apache) process, which is generally used as an entry-point for serving multiple applications on one domain.

Both of them need to listen to a port in order to be reachable.

What is usually done is to have an Apache or Nginx process listenning on default port 80, which is the only http port opened to external connections. And then use this Apache process to proxy your apps wich are running on other ports (like 5000, which should not be opened for external connections).

In your httpd conf, you could have an entry point on your domain, on the default port (80), and use a reverse proxy to reach you Node application on port 5000 :

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

This is usefull when you want to host multiple apps behind 1 port (usually 80), i.e. :

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>
like image 112
codename44 Avatar answered Oct 14 '22 05:10

codename44