Express

To run apps built with the Express web framework using Unit:

  1. Install Unit with the unit-dev/unit-devel package. Next, install Unit’s unit-http package. Run the following command as root:

    # npm install -g --unsafe-perm unit-http
    
  2. Create your app directory, install Express, and link unit-http:

    $ mkdir -p :/path/to/app/ # Path to the application directory; use a real path in your configuration
    
    $ cd /path/to/app/ # Path to the application directory; use a real path in your configuration
    
    $ npm install express --save
    

    Run the following command as root:

    # npm link unit-http
    
  3. Create your Express app; let’s store it as /path/to/app/app.js. First, initialize the directory:

    $ cd /path/to/app/ # Path to the application directory; use a real path in your configuration
    
    $ npm init
    

    Next, add your application code:

    #!/usr/bin/env node
    
    const http = require('http')
    const express = require('express')
    const app = express()
    
    app.get('/', (req, res) => res.send('Hello, Express on Unit!'))
    
    http.createServer(app).listen()
    

    The file should be made executable so the application can run on Unit:

    $ chmod +x app.js # Application file; use a real path in your configuration
    
  4. Change ownership:

    Run the following command (as root) so Unit can access the application directory (If the application uses several directories, run the command for each one):

    # chown -R unit:unit /path/to/app/  # User and group that Unit's router runs as by default
       
    Note:
    The unit:unit user-group pair is available only with official packages , Docker images, and some third-party repos. Otherwise, account names may differ; run the ps aux | grep unitd command to be sure.

    For further details, including permissions, see the security checklist.

  5. Next, prepare the Express configuration for Unit:

    {
       "listeners": {
          "*:80": {
             "pass": "applications/express"
          }
       },
       "applications": {
          "express": {
             "type": "external",
             "working_directory": "/path/to/app/",
             "working_directory_comment": "Needed to use the installed NPM modules; use a real path in your configuration",
             "executable": "/usr/bin/env",
             "executable_comment": "The external app type allows to run arbitrary executables, provided they establish communication with Unit",
             "arguments": [
             "node",
             "--loader",
             "unit-http/loader.mjs",
             "--require",
             "unit-http/loader",
             "app.js"
             ],
             "arguments_comment": "The env executable runs Node.js, supplying Unit's loader module and your app code as arguments",
             "app_js_comment": "Basename of the application file; be sure to make it executable"
          }
       }
    }
    
  6. Assuming the JSON above was added to config.json. Run the following command as root:

    # curl -X PUT --data-binary @config.json --unix-socket \
       /path/to/control.unit.sock \  # Path to Unit's control socket in your installation
       http://localhost/config/      # Path to the config section in Unit's control API
    
    Note:
    The control socket path may vary; run unitd -h or see Startup and shutdown for details.

After a successful update, your app should be available on the listener’s IP address and port:

Express on Unit - Welcome Screen


Last modified February 6, 2025