Example — REST API + PostgreSQL
A production-like REST API with Express, PostgreSQL, and proper error handling.
Project structure
my-api/
├── index.js
├── package.json
└── .gitignoreCode
package.json
{
"name": "my-api",
"version": "1.0.0",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.0",
"pg": "^8.11.0"
}
}index.js
const express = require('express');
const { Pool } = require('pg');
const app = express();
app.use(express.json());
// Database connection
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false },
});
// Create tables on startup
async function initDB() {
await pool.query(`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
)
`);
}
initDB();
// Routes
app.get('/users', async (req, res) => {
const { rows } = await pool.query('SELECT * FROM users ORDER BY created_at DESC');
res.json(rows);
});
app.get('/users/:id', async (req, res) => {
const { rows } = await pool.query('SELECT * FROM users WHERE id = $1', [req.params.id]);
if (rows.length === 0) return res.status(404).json({ error: 'User not found' });
res.json(rows[0]);
});
app.post('/users', async (req, res) => {
const { name, email } = req.body;
if (!name || !email) return res.status(400).json({ error: 'Name and email required' });
const { rows } = await pool.query(
'INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *',
[name, email]
);
res.status(201).json(rows[0]);
});
app.delete('/users/:id', async (req, res) => {
const { rowCount } = await pool.query('DELETE FROM users WHERE id = $1', [req.params.id]);
if (rowCount === 0) return res.status(404).json({ error: 'User not found' });
res.json({ message: 'Deleted' });
});
// Health check
app.get('/health', (req, res) => res.json({ status: 'ok' }));
const PORT = process.env.PORT || 3000;
app.listen(PORT, '0.0.0.0', () => {
console.log(`API running on port ${PORT}`);
});Console configuration
Configure the app in the console with GitHub source and the DATABASE_URL variable.
Deploy
- Push the code to a GitHub repository.
- In the console, choose Project Source as GitHub Repository.
- Select the main branch and create the project.
- Push to update the instance automatically.
Test it
Use the project URL on *.clients.zenifra.com to test the /users routes.
Expected responses
// POST /users → 201
{ "id": 1, "name": "Alice", "email": "[email protected]", "created_at": "2026-04-30T14:00:00.000Z" }
// GET /users → 200
[ { "id": 1, "name": "Alice", "email": "[email protected]", "created_at": "2026-04-30T14:00:00.000Z" } ]
// GET /users/999 → 404
{ "error": "User not found" }