Express TypeScript Service Template

Dec 1, 2023 | Programming

Template for new services based on Express and Typescript with the Best Practices and Ready for Production

nodejs node typescript npm swc swca docker

Motivation

Starting a new service in NodeJS can be a bit frustrating; there are lots of considerations to ensure we have a solid foundation to build upon. The primary goal of this template is to provide a robust base configuration for our NodeJS services, enabling us to deploy to production quickly and effectively.

What is Included in This Template?

  • Fully dockerized service ready for both development and production environments, prioritizing performance and minimal image size.
  • Utilizes SWC for compiling and testing the service.
  • Configured with Express as the HTTP framework.
  • Integrated with Husky to maintain code quality throughout development, including:
    • Running linter on modified files
    • Adopting conventional commits for consistency
    • Automatically executing tests
    • Checking for type errors using Typescript
    • Identifying grammatical typos
  • Testing capabilities with Vitest and Supertest for unit and e2e tests.
  • Performance testing through k6.
  • Combines unit and e2e test coverage, providing a holistic view of our tests.
  • Defines custom path aliases, allowing intuitive imports, e.g., using ‘@shared/logger’ instead of relative paths.
  • CICD setup with GitHub Actions, ensuring good code quality with valuable insights on dependencies and vulnerabilities.
  • Encourages the use of ESModules over CommonJS, embracing JavaScript standards.

Other Templates

If you’re considering starting new projects in other frameworks or creating a sophisticated library, explore these related templates:

Developing

To get started, create your .env file by duplicating the example:

bash
cp .env.example .env

This project is fully dockerized. To launch the app in development mode, run:

bash
docker-compose up -d my-service-dev

This mode supports hot-reload and exposes a debug port (9229), enabling IDE connections. For example, if you’re using VSCode, create a .vscode/launch.json file with this configuration:

{
  "version": "0.1.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach to docker",
      "restart": true,
      "port": 9229,
      "remoteRoot": "app"
    }
  ]
}

To run in production mode, execute:

bash
docker-compose up -d my-service-production

The service includes a health endpoint to verify functionality:

bash
curl --request GET --url http://localhost:3000/health

To stop the service, run:

bash
docker-compose down

Building

bash
npm run build

Testing

The service has various scripts for testing:

  • Run all tests: npm run test
  • Run unit tests: npm run test:unit
  • Run e2e tests: npm run test:e2e
  • For performance testing with k6, use:
    • Via Docker: docker-compose up k6
    • From your machine: brew install k6 and then npm run test:performance

Linting

To check code with the linter, run:

bash
npm run lint

To fix lint issues automatically, apply:

bash
npm run lint:fix

Troubleshooting

If you encounter any issues during setup or execution, consider the following:

  • Ensure that your Docker environment is correctly installed and running.
  • Check that your .env file is configured properly.
  • Ensure your installed Node.js version matches the version required in this template.
  • Look at any error logs generated in your container for specific errors.
  • For unexpected behaviors, try stopping and restarting your docker containers.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

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