node.jspassengerfastify

How to reverse port biding (Phusion Passenger and Fastify)?


I am trying to setup a node.js app on CPanel using Phusion Passenger. After some research I found that Phusion Passenger reverses control on the port binding. https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html

There are solutions for:

Express.js example:

if (typeof(PhusionPassenger) !== 'undefined') {
    PhusionPassenger.configure({ autoInstall: false });
}

var express = require('express');
var app = express();
app.get('/', function(req, res) {
    var body = 'Hello World';
    res.setHeader('Content-Type', 'text/plain');
    res.setHeader('Content-Length', body.length);
    res.end(body);
});

if (typeof(PhusionPassenger) !== 'undefined') {
    app.listen('passenger');
} else {
    app.listen(3000);
}

Is it possible to setup this use case in fastify? I have looked to FastifyListenOptions but don't know which property should I try out.

EDIT:

The problem was that the default behavior of fastify is to run host as 'localhost'. Phusion passenger can only call listen once. Since 'localhost' runs both IPv4 and IPv6 it crashes. Setting to '127.0.0.1' solves this. I asked this question on a wrong premise.


Solution

  • By running with fastify@4.5.3 it will work:

    const fastify = require('fastify')({ logger: true })
    fastify.get('/', async (request, reply) => {
      return { hello: 'world' }
    })
    
    if (typeof (PhusionPassenger) !== 'undefined') {
      fastify.listen({ path: 'passenger', host: '127.0.0.1' })
    } else {
      fastify.listen(8080)
    }
    

    Note that the listen option is forwarded to the http.createServer() method.

    passenger and fastify