Host a Node.js app with a database

Deploy an Express or Next.js app on DASTER from GitHub or a .zip, with PostgreSQL in the same project and DATABASE_URL given to the app at every deployment.

Published · DASTER

Last updated

To host a Node.js app with a database on DASTER, set up PostgreSQL in an App project, then deploy the app from GitHub or a .zip as a web service. DASTER reads package.json, installs with npm from your package-lock.json and runs your start script. At every deployment the app receives DATABASE_URL, so the code needs no connection string.

This post is for "host node js app" and "host next js app". It builds a small Express API that reads and writes PostgreSQL, shows the same idea in a Next.js route handler, and lists the settings DASTER expects.

What Node.js apps can DASTER run?

Hosting builds and runs Node and npm servers: Express, Fastify, a Next.js app with next start, or any server that listens on a port. It also serves static build output with Node. Each app runs in its own gVisor sandbox in Mumbai (ap-south-1) and answers at https://<app id>.apps.daster.io.

You needWhy
package.json with a start scriptDASTER fills the start command from it
package-lock.json, committedDependencies install with npm ci from the lockfile
npmpnpm and Yarn lockfiles are not supported yet
A port from 1024 to 65535The web service sends traffic to it

An app with dependencies and no package-lock.json fails with npm_lock_required. Run npm install once on your machine and commit the lockfile.

  1. Write the Express app

    package.json:

    JSON
    {
      "name": "todos-api",
      "private": true,
      "type": "module",
      "scripts": {
        "start": "node server.js"
      },
      "dependencies": {
        "express": "^5.1.0",
        "pg": "^8.13.1"
      }
    }

    server.js:

    JavaScript
    import express from 'express';
    import pg from 'pg';
    
    // DATABASE_URL comes from DASTER at every deployment, with sslmode=verify-full.
    const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL, max: 5 });
    
    const app = express();
    app.use(express.json());
    
    app.get('/health', (req, res) => {
      res.send('ok');
    });
    
    app.get('/todos', async (req, res) => {
      const { rows } = await pool.query('select id, title, done from app.todos order by id limit 20');
      res.json(rows);
    });
    
    app.post('/todos', async (req, res) => {
      const title = String(req.body?.title ?? '').trim();
      if (!title) return res.status(400).json({ error: 'title is required' });
      const { rows } = await pool.query(
        'insert into app.todos (title) values ($1) returning id, title, done',
        [title],
      );
      res.status(201).json(rows[0]);
    });
    
    const port = Number(process.env.PORT) || 3000;
    app.listen(port, () => console.log(`Listening on port ${port}`));

    Run npm install to create package-lock.json, then commit all three files. Express 5 passes errors from async handlers to its error handler, so a failed query answers 500 instead of crashing the server.

  2. Set up PostgreSQL

    Create an App project, open Backend Cloud and choose Set up Backend Cloud. Your organization's first PostgreSQL database is the smallest size (512 MB, 10 connections). Then open PostgreSQL › SQL Editor and create the table:

    SQL
    create table app.todos (
      id bigint generated always as identity primary key,
      title text not null,
      done boolean not null default false
    );

    Your tables live in the app schema, which is why the queries say app.todos.

  3. Deploy from GitHub or a .zip

    In Hosting, choose Deploy an app and pick Web service and a size. For the source, connect GitHub and pick the repository and branch, or choose Upload a .zip with the app folder (up to 100 MB; node_modules, .git and .env files are left out). DASTER inspects the code and fills in the settings. It runs nothing at this point.

    SettingValue for this app
    Install commandnpm ci
    Build commandempty
    Start commandnpm start
    Port3000
    Health check path/health

    Choose Review, then Deploy to Development, and follow the build to LIVE. Open https://<app id>.apps.daster.io/todos to see the list.

    With GitHub, Deploy on every push is on for the new service, so the next push to its branch deploys again.

  4. Check the connection

    Once Backend Cloud is ready, every deployment gives the app these variables:

    VariableWhat it is
    DATABASE_URLPostgreSQL through the session pooler, with sslmode=verify-full
    DASTER_URLThe address of your project's data API
    DASTER_SERVER_KEYThe project environment's server key

    pg reads sslmode=verify-full from the URL and checks the server's certificate. Keep the pool small: the smallest sizes have 10 connections, and each instance opens its own pool.

How do I host a Next.js app with the same database?

A Next.js app runs on DASTER as a Node server with next build and next start. Add pg to it and read the database in a route handler, app/api/todos/route.ts:

TypeScript
import { Pool } from 'pg';

// Read at request time: variables are not passed to `next build`.
export const dynamic = 'force-dynamic';

const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 5 });

export async function GET() {
  const { rows } = await pool.query('select id, title, done from app.todos order by id limit 20');
  return Response.json(rows);
}

Deploy it the same way, with npm run build as the build command, npm start as the start command and port 3000. Do not read DATABASE_URL while the app builds; NEXT_PUBLIC_* values are empty at build time too. The Next.js hosting guide covers previews, sizes and your own domain.

What should I know before I start?

  • The database is reachable only from apps hosted on DASTER. psql or an app hosted elsewhere cannot connect yet; use the SQL Editor or the data API.
  • Variables are not passed to build commands. Read settings at runtime.
  • Preview deployments never receive the connection, so a branch preview cannot touch your Development data.
  • Sizes: Starter (0.5 vCPU, 256 MB) or Standard (1 vCPU, 1 GB), with 1 or 2 instances, or Autoscale with a minimum of 0.
  • The wallet is checked first. Before a deploy starts, DASTER checks that your organization's wallet can pay the next hour of everything you run on Hosting. Prices are not final.

DASTER is in testing and sign-up is waitlist-only. Join the waitlist and you get access 20 days after you join.

Questions about hosting a Node.js app on DASTER

Which package managers work?

npm. DASTER installs with npm ci from package-lock.json. pnpm and Yarn are not supported yet, so generate a package-lock.json with npm install and commit it.

Can I deploy without GitHub?

Yes. Upload a .zip of the app folder, up to 100 MB, or deploy a public Docker image. To ship a new version of a .zip app, use Build & deploy settings › Deploy from a new .zip.

Do I have to copy DATABASE_URL into my variables?

No. An app hosted on DASTER receives DATABASE_URL, DASTER_URL and DASTER_SERVER_KEY at every deployment once Backend Cloud is ready. A variable you set yourself under one of these names is kept.

Can I use an ORM instead of pg?

Yes. Pass DATABASE_URL to any PostgreSQL driver or ORM that runs on Node and checks certificates with sslmode=verify-full. Remember to name the app schema, or set it on each new connection.

Can my Node app run background jobs?

Yes. Besides web services, Hosting runs workers, cron jobs on a schedule (UTC, minute precision) and functions that run when invoked, all in the same project with the same connection.

Next steps