Quickstart — With Database
Publish a Node.js application connected to a managed PostgreSQL database.
Note: This quickstart focuses on
PostgreSQL. ForMariaDB, use the guides dedicated to that engine.
Step 1 — Create the PostgreSQL database
- In the console, create a managed PostgreSQL database.
- Choose the desired version from the options shown in the console.
- Copy the full URI or the connection details shown.
- Keep the URI to configure the
DATABASE_URLvariable in the HTTP project.
Important: Zenifra does not inject
DATABASE_URLautomatically 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 dotenvCreate 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
- Create a repository on GitHub.
- In the console, choose
GitHub Repositoryas the Project Source. - In the runtime field, select
Node.js. - In the Port field, enter
3000. - Configure the
DATABASE_URLenvironment variable with the PostgreSQL URI created in step 1. - Configure the project's build and start commands.
- If you want automatic updates on push, enable
auto-deployduring creation. - Select the main branch and click Create Project.
- 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/healthCheck 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:
| Variable | Description |
|---|---|
DATABASE_URL | PostgreSQL connection string copied from the console |
Port | HTTP project creation field that must reflect the port used by the application |
ZENIFRA_INSTANCE_VERSION | Variable 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.