cd..blog

Full-Stack Fusion: Koa, Azure, & Heroku with TypeScript

const published = "Dec 11, 2025, 10:21 PM";const readTime = 5 min;
koatypescriptazureherokugit
Integrate Koa, Azure services, and Heroku deployment with TypeScript. Learn practical Git and build tool strategies for modern web apps.

Full-Stack Fusion: Koa, Azure, & Heroku with TypeScript

Modern web development often involves orchestrating a diverse set of tools and platforms to build robust, scalable applications. This post demonstrates how to integrate Koa for your API, TypeScript for type safety, Git for version control, efficient build tools, Heroku for seamless deployment, and Azure for powerful managed services.

1. The Core: Koa with TypeScript

Koa.js is a lightweight and powerful Node.js framework for building web APIs, leveraging ES2017 async/await for cleaner asynchronous code. TypeScript enhances Koa by providing static type checking, improving code quality and maintainability.

First, set up a basic Koa application in TypeScript.

// src/index.ts
import Koa from 'koa';
import Router from '@koa/router';
import { connectToCosmosDB } from './services/cosmosService'; // We'll define this later

const app = new Koa();
const router = new Router();

router.get('/', async (ctx) => {
  ctx.body = 'Hello from Koa on Heroku!';
});

router.get('/data', async (ctx) => {
  try {
    const container = await connectToCosmosDB();
    // Example: Fetching some data
    const { resources: items } = await container.items.query('SELECT * from c').fetchAll();
    ctx.body = items;
  } catch (error) {
    console.error('Error fetching data:', error);
    ctx.status = 500;
    ctx.body = 'Failed to fetch data from Azure Cosmos DB.';
  }
});

app.use(router.routes()).use(router.allowedMethods());

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

2. Build Tools & Bundlers (TypeScript Compiler)

TypeScript code needs to be compiled into JavaScript to run in Node.js. For a backend Koa application, the TypeScript compiler (tsc) is typically sufficient, transforming .ts files into .js.

Configure tsconfig.json for compilation and define build scripts in package.json.

// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}
// package.json (relevant scripts)
{
  "name": "koa-azure-heroku-ts",
  "version": "1.0.0",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "ts-node-dev --respawn src/index.ts"
  },
  "dependencies": {
    "@azure/cosmos": "^4.0.0",
    "@koa/router": "^12.0.0",
    "koa": "^2.15.0"
  },
  "devDependencies": {
    "@types/koa": "^2.13.0",
    "@types/koa__router": "^12.0.0",
    "@types/node": "^20.0.0",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.0.0"
  }
}

3. Version Control with Git

Git is essential for tracking changes, collaborating, and deploying code. It forms the backbone of modern CI/CD pipelines.

Ensure your project is initialized with Git and a .gitignore file is present to exclude build artifacts and sensitive information.

# .gitignore
node_modules/
dist/
.env

4. Azure for Managed Services

Microsoft Azure offers a vast array of managed services. Instead of hosting the entire app, we'll use Azure Cosmos DB, a globally distributed NoSQL database, as a backend data store for our Koa application.

// src/services/cosmosService.ts
import { CosmosClient, Container } from '@azure/cosmos';

let container: Container | undefined;

export async function connectToCosmosDB(): Promise<Container> {
  if (container) return container;

  const endpoint = process.env.AZURE_COSMOS_ENDPOINT;
  const key = process.env.AZURE_COSMOS_KEY;
  const databaseId = process.env.AZURE_COSMOS_DATABASE_ID;
  const containerId = process.env.AZURE_COSMOS_CONTAINER_ID;

  if (!endpoint || !key || !databaseId || !containerId) {
    throw new Error('Azure Cosmos DB environment variables are not set.');
  }

  const client = new CosmosClient({ endpoint, key });
  const database = client.database(databaseId);
  container = database.container(containerId);
  console.log('Connected to Azure Cosmos DB.');
  return container;
}

Set environment variables (AZURE_COSMOS_ENDPOINT, AZURE_COSMOS_KEY, etc.) in Heroku to securely connect your Koa app to Azure Cosmos DB.

5. Deployment with Heroku

Heroku provides a powerful Platform-as-a-Service (PaaS) that simplifies application deployment. Its Git-based deployment model integrates seamlessly with our workflow.

Create a Procfile at the root of your project to tell Heroku how to run your application.

// Procfile
web: npm run build && npm start

After committing your code and Procfile to Git, push to your Heroku remote. Heroku will automatically detect Node.js, run npm install, execute your build script, and then start your application using npm start.

Conclusion

By carefully integrating Koa and TypeScript for application logic, Git for version control, tsc for compilation, Azure for robust managed services like Cosmos DB, and Heroku for streamlined deployment, you create a powerful, maintainable, and scalable full-stack application. This hybrid approach leverages the strengths of each platform, providing a solid foundation for modern web development.