Skip to content

Free style Node.js structure

Hongli Lai edited this page Dec 6, 2013 · 5 revisions

We've received some concerns from Node.js users about Phusion Passenger enforcing a specific directory structure for Node.js applications. Passenger is under very active development, and we value community feedback, so we've addressed these concerns in version 4.0.25.

Starting from version 4.0.25, Passenger does not impose any structure, and you can use whatever structure you want with a bit of configuration. This demo shows you that Passenger can be very flexible.

Example

Normally, Passenger expects the entry point to be app.js, and expects a public and a tmp directory. But suppose that you have an application with the following structure that does not fit the Passenger-expected structure at all:

/webapps/foo
  |
  +-- server.js
  |
  +-- lib
  |
  +-- node_modules
  |
  +-- static_files

Here's how you can make this work.

In the Nginx mode:

server {
    listen 80;
    server_name www.foo.com;

    # Tell Passenger where the app is.
    passenger_app_root /webapps/foo;
    passenger_enabled on;

    # The entry point is server.js, not app.js. Tell Passenger
    # about this.
    passenger_app_type node;
    passenger_startup_file server.js;
    # The static assets are in `static_files` instead, so tell Nginx about it.
    root /webapps/foo/static_files;
    
    # There is no `tmp` dir. No problem, we can tell Passenger
    # to look for restart.txt in /webapps/foo instead.
    passenger_restart_dir /webapps/foo;
}

In the Standalone mode:

cd /webapps/foo
passenger start --app-type=node --startup-file=server.js --restart-dir=/webapps/foo --static-files-dir=/webapps/foo/static_files

Don't want Nginx to serve static files?

Set root to a nonexistant directory, like this:

root /nowhere;

or

--static-files-dir=/nowhere

Don't want to use restart.txt for restarting the app?

No problem! You can restart your app by looking for its PIDs in passenger-status, then killing them with kill. Passenger will respawn them either at the next request or within 5 seconds.