node.jshttpport80

deploy nodejs to http server on port 80


I have small node js code to be deployed on the http server listening on port 80. Below is the node.js server code listening on 8081.

var express = require('express'); // Web Framework
var app = express();
var sql = require('mssql'); // MS Sql Server client

// Connection string parameters.
var sqlConfig = {
    user: 'dbuser',
    password: 'dbpasswd',
    server: 'dbip',
    database : 'dbname',
    port:dbport
}




var server = app.listen(8081,'<serverip>', function () {
    var host = server.address().address
    var port = server.address().port

    console.log("app listening at http://%s:%s", host, port)
});

app.get('/table1', function (req, res) {

    sql.connect(sqlConfig, function(conerr,connection) {
      console.log(conerr);
       if (conerr) return;
         var request = new sql.Request();
        request.query('select * from table1', function(err, recordset) {
            if(err) console.log(err);
            console.log(recordset)
            res.end(JSON.stringify(recordset)); // Result in JSON format
        });
    });
})

The httpd conf file is as follows:

<VirtualHost *:80>
   ServerName <servername>
    <Directory "/var/www/html/njs/">
        AllowOverride All
        Require all granted
    </Directory>
            ProxyPass /njs/  http://<serverip>:8081/
            ProxyPassReverse /njs/ http://<serverip>:8081/
</VirtualHost>

When I start my node server and hit the browser with url 'http://serverip/njs/table1' , I get "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." .

Any idea whats wrong. I am a newbie to nodejs .Please help


Solution

  • It is always better to use Nginx when deploying a Node server. A detailed tutorial of how to set it up is provided in the link below(If you are using Linux). It is very easy to setup and very powerful.

    https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

    Edited -

    Here is an alternate solution to get node working in port 80.

    https://gist.github.com/kentbrew/776580

    You dont want any other server for that.