Skip to content

Node.js · deploy guide

Host a Node.js app with PostgreSQL

Run an Express (or any Node.js) server on Bashrack hosting from your GitHub repository, with a managed PostgreSQL database.

about 15 minutes of hands-on time

Who it’s for

Developers with a Node.js web server or API in GitHub.

You’ll need

  • A GitHub repository with package.json and a lockfile
  • Node.js 20 or newer

Steps

  1. step 1

    Listen on PORT

    We pass the port in the PORT variable. Listen on it and on all interfaces (0.0.0.0), not on localhost.

    js
    import express from "express";
    const app = express();
    app.get("/", (_req, res) => res.send("ok"));
    app.listen(Number(process.env.PORT ?? 3000), "0.0.0.0");
  2. step 2

    Dockerfile

    Install production dependencies only and run as the image's non-root node user.

    dockerfile
    FROM node:22-alpine
    WORKDIR /app
    COPY package*.json ./
    RUN npm ci --omit=dev
    COPY . .
    USER node
    EXPOSE 3000
    CMD ["node", "server.js"]
  3. step 3

    Read DATABASE_URL

    The connection string asks for an encrypted connection (sslmode=require). The postgres library reads it as is. With pg (node-postgres), drop the ?sslmode=require part from the URL and pass ssl: { rejectUnauthorized: false } instead.

    js
    import postgres from "postgres";
    const sql = postgres(process.env.DATABASE_URL);
    const [{ now }] = await sql`select now()`;
  4. step 4

    Create the app

    In the console, open Hosting → New app, connect GitHub and pick the repository and branch. Leave the port empty: after the first build we use the port your Dockerfile EXPOSEs. If you need a database, tick Add a PostgreSQL database in the same dialog.

  5. step 5

    Connect the database

    If the database was ordered separately, open it and choose Use in app: pick the app and the variable name. The app restarts with the variable set. Never commit the connection string to the repository.

Security and upkeep

  • Keep secrets in the app's environment variables, never in the repository
  • Run as the node user, not root
  • Commit the lockfile so every build installs the same versions

Official documentation