Run a Node.js app

nodejs, passenger, npm, express, app.js · 0 found this helpful

The Node.js and Python apps card in Developer runs a Node.js application behind your domain. You can host an Express API, a small server, or a Next.js app that has been built for production.

Before you start

  • Plan: a Node app counts toward CPU, memory, and entry-process limits. See Resource limits explained.
  • A domain or subdomain for the app. See Create a subdomain.
  • Your app code, with a package.json. Keep it outside public_html.

Create the app

  1. Open Account → Hosting → your site → Developer.
  2. Under Node.js and Python apps, click New app.
  3. Fill in the form:
Field Example
Runtime Node.js
Node.js version The newest LTS version on the list, such as 22
Address api.example.ca, with a path if you want one
Code folder apps/bakery-api (folder inside your home)
Startup file app.js
Mode Production
  1. Click Create app.
Node.js and Python apps in Developer

We make the folder and a virtual environment under ~/nodevenv/.

Add the code

Put these files in the application root. Use the file manager in Files, SFTP, or Git.

package.json:

{
  "name": "bakery-api",
  "version": "1.0.0",
  "main": "app.js",
  "dependencies": {
    "express": "^4.19.2"
  }
}

app.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ ok: true, shop: 'North Shore Bakery' });
});

app.get('/health', (req, res) => res.send('ok'));

// Passenger provides the port through process.env.PORT. A fallback helps local runs.
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on ${port}`));

Install packages

  1. Back in Developer, find your app under Node.js and Python apps.
  2. Click Install packages.
  3. Click Restart.

Or from SSH:

source /home/yourusername/nodevenv/apps/bakery-api/20/bin/activate && cd /home/yourusername/apps/bakery-api
npm install --omit=dev

Set environment variables

The Developer section doesn't have a screen for app variables yet. Keep them in a .env file outside your public folder, or open a request at https://barbrick.ca/submitticket.php and we'll set them for you, usually the same day. See Set environment variables for apps.

Example values:

NODE_ENV=production
DATABASE_URL=mysql://yourusername_shop:...@localhost/yourusername_bakery

Read them with process.env.DATABASE_URL.

Restart after changes

Click Restart each time you change code or variables. You can also touch a file:

mkdir -p ~/apps/bakery-api/tmp && touch ~/apps/bakery-api/tmp/restart.txt

Check

Open https://api.example.ca/health. You should see ok.

Logs

Errors go to stderr.log in the app root.

tail -n 50 ~/apps/bakery-api/stderr.log

Notes and limits

  • Don't use app.listen with a fixed port number. Use process.env.PORT.
  • Build tools (Webpack, Next build) use a lot of memory. Build on your computer, and upload the output.
  • The app stops when idle and restarts on a request, which gives the first request a short delay.
  • If your app needs WebSockets or a long-running background worker, ask us before you build on it, and we'll check it suits your plan.

If something goes wrong

Problem Fix
503 or "Incomplete response" The app crashed. Read stderr.log
Cannot find module Click Install packages, then Restart
Works locally, not here Different Node version. Match the version
"Resource limit reached" See Resource limits explained
Static files 404 Serve them in Express with express.static

Was this helpful?

Still stuck? Ask us and we'll walk you through it.