How to serve static files in a Node.js selector app?

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cbolt
    Junior Member
    • Aug 2025
    • 2

    #1

    How to serve static files in a Node.js selector app?

    One of my hosting customers has asked how to serve static files (such as images, css etc) in a Node.js selector app?
    The passenger documentation says that files in the public/ sub folder will be statically served but this isn't happening.
    When I create a node.js app in the selector it automatically creates the public sub folder but serving of static files does not work
    Can anyone advise me on this please so I can help my customer out?
  • Answer selected by bogdan.sh at 08-11-2025, 05:37 AM.
    bogdan.sh
    Administrator
    • Nov 2016
    • 1239

    This is a classic snag when running a Node.js app in a subdirectory on cPanel. It's frustrating, but the fix is all about telling the server how to properly route requests to your app.

    I've just made it working using the 'express' module on my test server (and with a help of our Virtual Assistants which is available in docs or on support portal).

    Here's a step-by-step guide that should get you sorted.

    Log into your server as a user, activate venv (in my case it was):
    Code:
    source /home/bogdant/nodevenv/statictest/10/bin/activate && cd /home/bogdant/statictest
    SInce I tested with nodeJS 10 the express v4 is needed, so:

    Code:
    npm install express@4
    Then this simple code in app.js or whatever your app startup file is will do the trick:

    Code:
    const express = require('express');
    const path = require('path');
    const app = express();
    const router = express.Router();
    
    // This serves files from the 'public' directory, relative to the router's base path
    router.use(express.static(path.join(__dirname, 'public')));
    
    router.get('/', (req, res) => {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        const message = 'It works from the router!\n';
        const version = 'NodeJS ' + process.versions.node + '\n';
        const response = [message, version].join('\n');
        res.end(response);
    });
    
    // Tell the main app to use this router for all requests starting with /statictest
    app.use('/statictest', router);
    
    // Phusion Passenger will listen on the correct port
    app.listen();
    And accessing just domain.com/statictest/CLOS-184x40.png show the static file properly.
    Last edited by bogdan.sh; 08-11-2025, 05:38 AM.

    Comment

    • bogdan.sh
      Administrator
      • Nov 2016
      • 1239

      #2
      This is a classic snag when running a Node.js app in a subdirectory on cPanel. It's frustrating, but the fix is all about telling the server how to properly route requests to your app.

      I've just made it working using the 'express' module on my test server (and with a help of our Virtual Assistants which is available in docs or on support portal).

      Here's a step-by-step guide that should get you sorted.

      Log into your server as a user, activate venv (in my case it was):
      Code:
      source /home/bogdant/nodevenv/statictest/10/bin/activate && cd /home/bogdant/statictest
      SInce I tested with nodeJS 10 the express v4 is needed, so:

      Code:
      npm install express@4
      Then this simple code in app.js or whatever your app startup file is will do the trick:

      Code:
      const express = require('express');
      const path = require('path');
      const app = express();
      const router = express.Router();
      
      // This serves files from the 'public' directory, relative to the router's base path
      router.use(express.static(path.join(__dirname, 'public')));
      
      router.get('/', (req, res) => {
          res.writeHead(200, { 'Content-Type': 'text/plain' });
          const message = 'It works from the router!\n';
          const version = 'NodeJS ' + process.versions.node + '\n';
          const response = [message, version].join('\n');
          res.end(response);
      });
      
      // Tell the main app to use this router for all requests starting with /statictest
      app.use('/statictest', router);
      
      // Phusion Passenger will listen on the correct port
      app.listen();
      And accessing just domain.com/statictest/CLOS-184x40.png show the static file properly.
      Last edited by bogdan.sh; 08-11-2025, 05:38 AM.

      Comment

      • cbolt
        Junior Member
        • Aug 2025
        • 2

        #3
        Thanks for the detailed response, this was very helpful!

        Comment

        Working...