Getting Started with the TypeScript API Starter

Apr 8, 2022 | Programming

Welcome to the world of building applications using cutting-edge technology! In this guide, we’ll explore how to set up a Node.js and Express.js API with TypeScript and PostgreSQL using the typescript-api-starter template. Not only will you learn how to kickstart your project, but you will also get insights into important tools and commands necessary for smooth sailing.

Requirements

Before diving into the setup, ensure you have the following tools installed:

Getting Started

To get your TypeScript API up and running, follow these simple steps:

bash
# Clone repository
$ git clone git@github.com:cham11ng/typescript-api-starter.git application-name

# Navigate into the application
$ cd application-name

# Update database credentials
$ cp .env.example .env

# Install dependencies
$ yarn install

# Perform migration
$ yarn migrate

# Load fake data in the database
$ yarn load:fake

Now, you’re ready to start the application!

Starting the Application

You can start the application either in production or development mode:

  • For production: $ yarn build
  • For development: $ yarn dev

Make sure to check the operational status through the logs after starting.

Utilizing Docker

If you prefer containerized applications, Docker is here to help! Follow these steps:

bash
# Start the Docker services
$ docker compose up -d api

# Check if the server has started before proceeding
$ docker compose exec api sh yarn migrate

# View logs of the container
$ docker compose logs -f api

# To stop the services
$ docker compose stop api postgres

Generating Migrations and Seeds

To manage your database, you may need to create migrations and seeds. Here’s how:

bash
# To create a migration
$ yarn make:migration create_table_name_table

# To create a seed
$ yarn make:seeder table_name_table_seeder

# Example
$ yarn make:migration create_posts_table
$ yarn make:seeder post_table_seeder

# Finally
$ yarn migrate  # to apply migrations
$ yarn seed     # to load seed data

Setting Up REST Client

For seamless API requests, you can set up a REST client in Visual Studio Code:

json
rest-client.environmentVariables: {
    $shared: {
        refreshToken: "foo",
        accessToken: "bar",
        email: "sgr.raee@gmail.com",
        password: "secret"
    },
    local: {
        host: "localhost",
        refreshToken: "$shared.refreshToken",
        accessToken: "$shared.accessToken",
        email: "$shared.email",
        password: "$shared.password"
    }
}

Troubleshooting

Here are some common issues you might face along with potential solutions:

  • If the application fails to start, ensure you have migrated the database correctly.
  • If you encounter permission errors, check your Docker settings to ensure it has the necessary permissions.
  • If dependencies are not installing, make sure you have the correct Node.js version installed.

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.

Happy coding!

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

Tech News and Blog Highlights, Straight to Your Inbox