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.
- You must use npm v7 or posterior versions.This repository uses version 2 of
- 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
- Create a new application with CodeNOW (e.g., named
express-api
). - Add a component in that application (e.g., named
rest-api-with-postgres
) with the Docker Generic stack. - Clone the component's repository to your computer.
- Clone the template repository to your computer.
- Copy the contents of this repository (except the
.git
file) into the cloned component's repository.
Step 2: run locally
- In a terminal on your local computer, from the root of your repository, type
npm ci
to install all the application dependencies. We recommendnpm ci
overnpm install
(or the equivalentnpm i
) in order not to modify inadvertently thepackage-lock.json
. - Start an instance of a PostgreSQL server (e.g.,
psql -U postgres
or use pgadmin v4 which should come preinstalled with Postgres). - Type
npm start
to start the application.
Step 3: test locally
-
GET
Routes- Visit http://localhost:3000
/messages
/messages/1
/users
/users/1
- Visit http://localhost:3000
-
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
- Create a message with:
- 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" }
- URL:
- Delete a message with:
- URL:
http://localhost:3000/messages/1
- Method:
DELETE
- URL:
- CURL
Step 4: create a Postgres database in CodeNOW
- Add a postgreSQL (v12) managed service (Marketplace > Managed services > Get New Service).
Step 5: connect the app to the database
- Create a new connection for your component:
- Name your connection (e.g.,
EXPRESS_DEMO_DB
). - Select PostgreSQL as the service type.
- Name your connection (e.g.,
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
- Commit and push to the component's remote repository.
- Build your component.
- Create an application package that contains the newly built component.
Step 7: deploy your application
- Create a deployment configuration for your target deployment environment and package.
- Edit the
.env
as you see fit to pass environment variables to your NodeJS application. - Deploy your application:
- In step 1 of the deployment process, pick the target deployment and package which you created a deployment configuration for.
- In step 2, review and possibly update your configuration files.
- 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.