Express is a great minimal and flexible web application framework.

Together with PostgreSQL database it can be a great choice for building scalable and maintainable backend for many kinds of apps.

In this tutorial we will see how you can create an express project with a single endpoint which will store data in PostgreSQL database and how to deploy said project to AWS EC2 instance and Digital Ocean Droplet.

If you are looking for other deployment tutorials checkout out one of these:

In this post:

Create Express app

  1. Make sure you have Node.js and NPM installed in your system
  2. Create a directory for the project and navigate to it
   mkdir express_demo
   cd express_demo
  1. Initialise the npm project with npm init and follow the prompts (you can hit enter to all of them accepting default values) image
  2. Install required packages: Express.js and postgresql client package:
npm install express pg
  1. Create the express.js app: create a new file index.js
  2. Inside index.js setup the express app and the database connection
const express = require('express');  
const { Pool } = require('pg');  

const app = express();  
const port = process.env.PORT || 3000;

// Database connection  
const pool = new Pool({  
  connectionString: process.env.DATABASE_URL,  
});  

// Middleware to parse JSON data  
app.use(express.json());  

// Function to create "Messages" table if it doesn't exist  
async function createTableIfNotExists() {  
  try {  
    const createTableQuery = `  
      CREATE TABLE IF NOT EXISTS Messages (  
        id SERIAL PRIMARY KEY,        name VARCHAR NOT NULL,        email VARCHAR NOT NULL,        message TEXT NOT NULL      )  
    `;  

    const client = await pool.connect();  
    await client.query(createTableQuery);  
    client.release();  
    console.log('Table "Messages" created or already exists.');  
  } catch (error) {  
    console.error('Error creating table:', error);  
  }  
}  


// Endpoint to handle POST requests  
app.post('/messages', async (req, res) => {  
  try {  
    const { name, email, message } = req.body;  

    // Insert the data into the database  
    const insertQuery = `  
      INSERT INTO Messages (name, email, message)  
      VALUES ($1, $2, $3)  
      RETURNING *    `;  
    const values = [name, email, message];  

    const client = await pool.connect();  
    const result = await client.query(insertQuery, values);  
    client.release();  

    res.status(201).json(result.rows[0]);  
  } catch (error) {  
    console.error('Error storing data:', error);  
    res.status(500).json({ error: 'Internal Server Error' });  
  }  
});  

// Start the server and create table before listening  
(async () => {  
  try {  
    await createTableIfNotExists();  
    app.listen(port, () => {  
      console.log(`Server is running on http://localhost:${port}`);  
    });  
  } catch (error) {  
    console.error('Error starting the server:', error);  
  }  
})();

Worth a note that we expect the DATABASE_URL environment variable to have database connection string.

The createTableIfNotExists function will create database table if it is not present in the database yet on app startup.

The endpoint /messages accepts post requests and will store them in the database. 7. Edit package.json to include a scripts entry start to run the app, and the build entry, because it is expected by Appliku by default, but don't actually have anything to build right now, so the file looks like this:

{
  "name": "express_demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "build": "echo 'Skipping build process'",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "pg": "^8.11.1"
  }
}
  1. Create Procfile that will tell Appliku how to run your app
web: yarn start
  1. Create a .gitignore file which will help prevent unwanted files to get added to the version control
node_modules/*  
.idea  
.vscode  
.DS_Store
  1. Commit the code
git init
git add .
git commit -m'Initial Commit'
  1. Create a GitHub or GitLab repository and push your code there.

Deploy Express app

In order to deploy your app you will need two things: - Appliku account which you can get here: https://app.appliku.com - An account at a cloud provider of your choice. In this article we'll talk about AWS and Digital Ocean

Deploy Express app on AWS EC2 Instance

AWS is a popular cloud provider that offers a huge number of services for building and running apps of all sorts. Get your account here: https://portal.aws.amazon.com/billing/signup#/start/email

In this section we'll create an EC2 Instance and deploy your Express app on it.

In order to deploy Express app on AWS EC2 you need to generate AWS keys first. Follow instructions in this guide

After you have keys setup open Appliku dashboard and go to the "Servers" tab.

Select AWS provider.

image

Select the region, instance type and the disk size. To fit in AWS Free Tier make sure the disk size to be <30GB.

image

image

Click "Create EC2 Instance".

You will be taken to the server page where you will see the setup process. When it is finished, go to Applications tab and create a new app from GitHub (or GitLab if you hosted your repository there).

Deploy Express app on Digital Ocean

If you choose to go with Digital Ocean first make sure to create a DO account.

You will need to generate a token here https://cloud.digitalocean.com/account/api/tokens/new

Make sure to select scopes both "Read" and "Write". We recommend to setup expiration to "Never" or "1 Year" to avoid service interruptions.

image

In Appliku dashboard go to Servers tab and click "Setup Credentials" for Digital Ocean. Paste and save the token and go back to Servers tab. You will be able to select Digital Ocean provider now.

image

Select the region and Droplet Type. For Droplet Size we strongly recommend to pick at least 1GB RAM instance so your server can build and run your app and the database.

Click "Create Droplet". image

Appliku will call Digital Ocean API and create a server for you and begin setup.

When that is finished you will be able to create the app and the database.

Deploy Express app with Appliku

Click on the "Applications" menu link.

image

If you haven't setup connection to will need to do that by clicking Setup Credentials.

When you are done you will be able to select the service you prefer. For this tutorial we'll go with GitHub

image

Give your app a name, select repository, branch and the server you have just created and click "Create Application".

image

Click on "Manage" in the databases block and add a database "Postgres 15" on the server we have just created.

image

image

Wait for it to change status to "Deployed".

image

Go back to application overview page and click Settings.

image

Change Base Docker Image to "Node 20 Yarn"

image

Get back to application overview page and click "Deploy Now"

image

You can see that app deployment succeeded and the process on the right appeared showing the current resource consumption by the app

image

Now you can click on "View Application" to open our app.

image

If something went wrong and your app is not responding correctly then you will see 502 error.

If that's the case, click on "App Logs" to find the reason.

image

What we see now is the actual app responding that there is no route for the root URL. That's perfectly fine.

We only have a single endpoint that expects POST requests.

Open terminal and run the following command to send a post request with curl .

curl --request POST \
  --url https://expressdemo.applikuapp.com/messages \
  --header 'Content-Type: application/json' \
  --data '{
"name" : "john Snow",
"email": "123@example.com",
"message": "hello"
}'

image

Awesome! our message was stored in the database and has "id": 1.

Let's go back to Appliku dashboard on the database page and get credentials for our database:

image

Click on "Show Credentials"

image

Database connection URL will appear under the database information. Copy it and connect to the database.

image

I'll use terminal and psql command line:

psql postgresql://qjgnkoditbntvkdx:aeyeqeqwgbnbnsji@3.75.209.3:8000/zithhsqqzbmttyuc

image

Congratulations! Our Express app is deployed and working well with PostgreSQL!