Quickstart

Quickstart — With Database

Publish a Node.js application connected to a managed PostgreSQL database.

Note: This quickstart focuses on PostgreSQL. For MariaDB, use the guides dedicated to that engine.

Step 1 — Create the PostgreSQL database

  1. In the console, create a managed PostgreSQL database.
  2. Choose the desired version from the options shown in the console.
  3. Copy the full URI or the connection details shown.
  4. Keep the URI to configure the DATABASE_URL variable in the HTTP project.

Important: Zenifra does not inject DATABASE_URL automatically into your application. This configuration must be done manually in the HTTP project before publishing or in a new deploy.

Step 2 — Create your application

mkdir zenifra-api
cd zenifra-api
npm init -y
npm install express pg dotenv

Create index.js:

const express = require('express');
const { Pool } = require('pg');
const app = express();
app.use(express.json());

// Connect to managed PostgreSQL using the ENV configured manually in the console
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

app.get('/health', (req, res) => {
  res.json({ status: 'ok' });
});

app.get('/', async (req, res, next) => {
  let client;

  try {
    client = await pool.connect();
    const { rows } = await client.query('SELECT NOW() as now');
    res.json({ database_time: rows[0].now });
  } catch (error) {
    next(error);
  } finally {
    if (client) client.release();
  }
});

app.post('/todos', async (req, res, next) => {
  const { task } = req.body;
  let client;

  try {
    client = await pool.connect();
    await client.query(
      'CREATE TABLE IF NOT EXISTS todos (id SERIAL PRIMARY KEY, task TEXT, done BOOLEAN DEFAULT false)'
    );
    const { rows } = await client.query('INSERT INTO todos (task) VALUES ($1) RETURNING *', [task]);
    res.status(201).json(rows[0]);
  } catch (error) {
    next(error);
  } finally {
    if (client) client.release();
  }
});

app.use((error, req, res, next) => {
  console.error(error);
  res.status(500).json({ error: 'Internal server error' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, '0.0.0.0', () => {
  console.log(`API running on port ${PORT}`);
});

Step 3 — Publish via GitHub

  1. Create a repository on GitHub.
  2. In the console, choose GitHub Repository as the Project Source.
  3. In the runtime field, select Node.js.
  4. In the Port field, enter 3000.
  5. Configure the DATABASE_URL environment variable with the PostgreSQL URI created in step 1.
  6. Configure the project's build and start commands.
  7. If you want automatic updates on push, enable auto-deploy during creation.
  8. Select the main branch and click Create Project.
  9. Push the code to the selected branch.

If the HTTP project already exists, save the DATABASE_URL variable in it and publish a new version so the application reads the updated ENV.

Step 4 — Test

Use the project URL at *.clients.zenifra.com and validate the application.

Check if the application is online:

curl https://your-project.clients.zenifra.com/health

Check the database connection:

curl https://your-project.clients.zenifra.com/

Create an item to validate PostgreSQL writes:

curl -X POST https://your-project.clients.zenifra.com/todos \
  -H 'Content-Type: application/json' \
  -d '{"task":"Build something cool"}'

Response:

{ "id": 1, "task": "Build something cool", "done": false }

Environment variables

For this flow, configure at least these variables and fields manually:

VariableDescription
DATABASE_URLPostgreSQL connection string copied from the console
PortHTTP project creation field that must reflect the port used by the application
ZENIFRA_INSTANCE_VERSIONVariable injected by Zenifra with the deployed instance version

Database access

Use the full URI or the connection details provided in the console to connect your application or external tools to the managed PostgreSQL database.