Getting Started with the Express Postgres Starter Project

Jul 6, 2024 | Programming

Welcome to the Express Postgres Starter project! This guide will walk you through setting up a Node.js application using Express and Postgres with Docker, and ensure you have a solid foundation for building and managing your web application. Let’s get rolling!

Setup Prerequisites

  • Ensure you have Docker for Desktop installed on your machine.

Launching the Application

To start your application, follow these steps:

  1. Open your terminal and navigate to the root of your project.
  2. Run the following command:
  3. docker-compose up
  4. This command will initiate Docker, bringing up both Postgres and the Express application server.
  5. By default, the application will be accessible via localhost:3000. You can change this by modifying the first 3000 in 3000:3000 within the .docker-compose.yaml file.
  6. Postgres can be accessed through port 35432. Your connection string will look like this: postgres:user:pass@localhost:35432/db (where username, password, and database name are defined in the .docker-compose.yaml).
  7. To connect to Postgres using the psql client, run:
  8. sh
    psql postgres:user:pass@localhost:35432/db

Understanding Application Behavior

The default command for the Docker container is to run npm start. However, the .docker-compose.yaml file overrides this to npm run dev, which uses nodemon for auto-restarting the app upon code changes. This keeps the development flow smooth and efficient.

Setting Up the Express API

Your Express API resides within the .src/api directory. Here’s a brief overview of how it’s structured:

  • Application routes for resources are defined in .src/api/index.js.
  • Global concerns such as security, cookie parsing, body parsing, and request logging are handled in .server.js.

This application loosely follows the Presentation Domain Data Layering principle, where:

  • Presentation logic is located in the src/api folder.
  • Domain logic is intended to be in the src/modules folder, which is currently non-existent since we only have generic user and session resources.
  • Data persistence is handled in the src/persistence folder.

Database Setup and Management

To manage your database effectively, you can use the following commands:

  • Run migrations: npm run migrate up
  • Roll back migrations: npm run migrate down
  • Create a new migration file: npm run migrate:create migration-name (this will generate a file in .src/migrations).

To run migrations inside the Docker container, execute:

sh
docker-compose run app bash
npm run migrate up

Session Management and Authentication

Session management is achieved through a custom sessions table and the .src/api/session.js file, leveraging client-sessions for authentication.

Troubleshooting

Should you encounter any issues while setting up the project, here are a few recommendations:

  • If you face connectivity issues with Postgres, check to ensure Docker is running properly and your ports are correctly mapped in the .docker-compose.yaml file.
  • For any problems related to the Express server not starting, ensure that the dependencies are correctly installed by running npm install.
  • If migrations are failing to create or run, review your migration files for any syntax errors and confirm that your database connection is established.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox