Skip to main content

NodeJS/Express API with Postgres

Create an API with NodeJS, Express, and Postgres quickly by starting from a working, deployable example in CodeNOW. With the Docker Generic scaffolder, developers can create a Dockerfile that sets up a NodeJS Express application. Developers can then create an instance of Postgres managed service in CodeNOW and connect it to their application to implement the API logic.

The code for this repository is largely inspired by an excellent introductory blog post by Robin Wieruch.

Prerequisites

  • node & npm
    • You must use npm v7 or posterior versions.This repository uses version 2 of package-lock.json. While version 2 is backward compatible with version 1 (used by npm v5 and v6), you should upgrade your local npm version to at least v7 to avoid possible issues.
    • CodeNOW's deployment will use Node v18 (cf. Dockerfile). To have deployment environments as close as possible to your local environment, we recommend you to use the same node version.
  • git is installed.
  • To test locally, you must have PostgreSQL installed. To install PostgreSQL, refer to its download page. We recommend installing the same PostgreSQL version that is used in CodeNOW. At the time of this writing, this is v12.11.
  • Make sure all your environment variables (e.g., path) are correctly set, so you can run executables from the terminal (e.g., typing psql -U postgres in a terminal should start a Postgres server).
  • Basic knowledge of relational databases in general and Postgres in particular.

Steps

Step 1: install locally

  1. Create a new application with CodeNOW (e.g., named express-api).
  2. Add a component in that application (e.g., named rest-api-with-postgres) with the Docker Generic stack.
  3. Clone the component's repository to your computer.
  4. Clone the template repository to your computer.
  5. Copy the contents of this repository (except the .git file) into the cloned component's repository.

Step 2: run locally

  1. In a terminal on your local computer, from the root of your repository, type npm ci to install all the application dependencies. We recommend npm ci over npm install (or the equivalent npm i) in order not to modify inadvertently the package-lock.json.
  2. Start an instance of a PostgreSQL server (e.g., psql -U postgres or use pgadmin v4 which should come preinstalled with Postgres).
  3. Type npm start to start the application.

Step 3: test locally

  • GET Routes

    • Visit http://localhost:3000
      • /messages
      • /messages/1
      • /users
      • /users/1
  • Beyond GET Routes

    • CURL
      • Create a message with:
        • curl -X POST -H "Content-Type:application/json" http://localhost:3000/messages -d '{"text":"Hi again, World"}'
      • Delete a message with:
        • curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/messages/1
    • Postman
      • Install Postman to interact with the REST API
      • Create a message with:
        • URL: http://localhost:3000/messages
        • Method: POST
        • Body: raw + JSON (application/json)
        • Body Content: { "text": "Hi again, World" }
      • Delete a message with:
        • URL: http://localhost:3000/messages/1
        • Method: DELETE

Step 4: create a Postgres database in CodeNOW

  1. Add a postgreSQL (v12) managed service (Marketplace > Managed services > Get New Service).

Step 5: connect the app to the database

  1. Create a new connection for your component:
    • Name your connection (e.g., EXPRESS_DEMO_DB).
    • Select PostgreSQL as the service type.

Note that if you name your connection anything else than EXPRESS_DEMO_DB, you need to update the name of the environment variables used in the code to connect to the database (cf. /src/models/index.js).

Step 6: build and package your application

  1. Commit and push to the component's remote repository.
  2. Build your component.
  3. Create an application package that contains the newly built component.

Step 7: deploy your application

  1. Create a deployment configuration for your target deployment environment and package.
  2. Edit the .env as you see fit to pass environment variables to your NodeJS application.
  3. Deploy your application:
    1. In step 1 of the deployment process, pick the target deployment and package which you created a deployment configuration for.
    2. In step 2, review and possibly update your configuration files.
    3. In step 5, select the specific instance of managed service that you want to connect to the soon-to-be-deployed instance(s) of your application.