Node.js TCP server on Plesk not starting, "Server started on Port undefined" error

65 Views Asked by At

I'm trying to run a Node.js TCP server on a Plesk server, but I'm encountering issues. The server doesn't start, and in the Passenger logs, I see the error backtrace:
in 'bool Passenger::SpawningKit::HandshakePerform::checkCurrentState()' (Perform.h:257)
in 'void Passenger::SpawningKit::HandshakePerform::waitUntilSpawningFinished(boost: :unique_lock<boost::mutex>&)' (Perform.h:213)
in 'Passenger::SpawningKit::Result Passenger::SpawningKit::HandshakePerform::execute()' (Perform.h:1752)
in 'Passenger::SpawningKit::Result Passenger::SpawningKit::DirectSpawner::internalSpawn(const AppPoolOptions&, Passenger::SpawningKit::Config&, Passenger::SpawningKit::HandshakeSession&, const Passenger::Json::Value&, Passenger::SpawningKit::JourneyStep&)' (DirectSpawner.h:211)
in 'virtual Passenger::SpawningKit::Result Passenger::SpawningKit::DirectSpawner::spawn(const AppPoolOptions&)' (DirectSpawner.h:261)
in 'void Passenger::ApplicationPool2::Group::spawnThreadRealMain(const SpawnerPtr&, const Passenger::ApplicationPool2::Options&, unsigned int)' (SpawningAndRestarting.cpp:95)

Stdout and stderr output:
Server started on Port undefined

The NodeJS code:

const net = require('net');

const serverPort = process.env.PORT;
const clients = {};

const server = net.createServer((client) => {
  console.log('New client connected');

  // Generate a random client ID
  const clientId = Math.random().toString(36).substring(7);
  clients[clientId] = client;

  // Greet the client with their ID
  client.write(`Welcome, your client ID is: ${clientId}\r\n`);

  client.on('data', (data) => {
    const request = data.toString().trim();
    console.log(`Message from ${clientId}: ${request}`);

    // Send the message to all other clients
    Object.keys(clients).forEach((otherClientId) => {
      if (otherClientId !== clientId) {
        clients[otherClientId].write(`\nMessage from ${clientId}: ${request}\n`);
      }
    });
  });

  client.on('end', () => {
    console.log(`Client ${clientId} disconnected`);
    // Remove the client from the list of connected clients
    delete clients[clientId];
  });

  client.on('error', (err) => {
    console.log(`Client ${clientId} caused an error: ${err.message}`);
    // Remove the client from the list of connected clients
    delete clients[clientId];
  });
});

server.listen(serverPort, () => {
  console.log(`Server started on port ${serverPort}`);
});

I suspect there might be a problem with setting the PORT environment variable in Plesk. I've tried setting it directly during script execution, but it doesn't seem to work. I can't find an option in Plesk to set the environment variable, since NodeJS applications are processed via Passenger. How can I correctly configure the environment variable for a Node.js server on Plesk?
Everything works wonderfully in localhost. So I don't think it's the code.

Any help or insights into resolving this issue would be greatly appreciated!

Try:

  1. Set the serverPort variable directly to 3000 inside code
  2. Set the PORT variable to 3000 with plesk custom environment variables for this nodeJS project
  3. Tested the code in local network (worked)

Expecting:

  1. n clients can connect, e.g. via telnet
  2. Sent messages are forwarded to everyone except yourself
  3. If a client disconnects, it is removed from the clients array
  4. If a client has a error, it is removed from the clients array
0

There are 0 best solutions below