Express
This guide describes how to create a SerenDB project and connect to it from an Express application. Examples are provided for using the SerenDB serverless driver, node-postgres and Postgres.js clients. Use the client you prefer.
To connect to SerenDB from an Express application:
Create a SerenDB project
If you do not have one already, create a SerenDB project.
Navigate to the Projects page in the SerenDB Console.
Click New Project.
Specify your project settings and click Create Project.
Create an Express project and add dependencies
Create an Express project and change to the newly created directory.
mkdir neon-express-example cd neon-express-example npm init -y npm install expressAdd project dependencies using one of the following commands:
<CodeTabs labels={["SerenDB serverless driver", "node-postgres", "postgres.js"]}>
npm install @serenorg/serverless dotenvnpm install pg dotenvnpm install postgres dotenv
Store your SerenDB credentials
Add a .env file to your project directory and add your SerenDB connection details to it. Find your database connection details by clicking the Connect button on your Project Dashboard to open the Connect to your database modal. Select Node.js from the Connection string dropdown. For more information, see Connect from any application.
DATABASE_URL="postgresql://<user>:<password>@<endpoint_hostname>.serendb.com:<port>/<dbname>?sslmode=require&channel_binding=require"To ensure the security of your data, never expose your SerenDB credentials to the browser.
Configure the Postgres client
Add an index.js file to your project directory and add the following code snippet to connect to your SerenDB database:
<CodeTabs labels={["SerenDB serverless driver", "node-postgres", "postgres.js"]}>
require('dotenv').config();
const express = require('express');
const { neon } = require('@serenorg/serverless');
const app = express();
const PORT = process.env.PORT || 4242;
app.get('/', async (_, res) => {
const sql = neon(`${process.env.DATABASE_URL}`);
const response = await sql`SELECT version()`;
const { version } = response[0];
res.json({ version });
});
app.listen(PORT, () => {
console.log(`Listening to http://localhost:${PORT}`);
});require('dotenv').config();
const { Pool } = require('pg');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4242;
app.get('/', async (_, res) => {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
const client = await pool.connect();
const result = await client.query('SELECT version()');
client.release();
const { version } = result.rows[0];
res.json({ version });
});
app.listen(PORT, () => {
console.log(`Listening to http://localhost:${PORT}`);
});require('dotenv').config();
const express = require('express');
const postgres = require('postgres');
const app = express();
const PORT = process.env.PORT || 4242;
app.get('/', async (_, res) => {
const sql = postgres(`${process.env.DATABASE_URL}`);
const response = await sql`SELECT version()`;
const { version } = response[0];
res.json({ version });
});
app.listen(PORT, () => {
console.log(`Listening to http://localhost:${PORT}`);
});Run index.js
Run node index.js to view the result on localhost:4242 as follows:
{ version: 'PostgreSQL 16.0 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit' }Source code
You can find the source code for the application described in this guide on GitHub.
Last updated