Blog
Jun 8, 2025 - 4 MIN READ
Quick Start: Setting Up an Express Server with TypeScript

Quick Start: Setting Up an Express Server with TypeScript

A simple guide to setting up a basic Express.js server with TypeScript for better development experience and type safety.

Pheak Minute

Pheak Minute

Setting up an Express server with TypeScript is a straightforward process that enhances your development experience with type safety. This guide will walk you through the minimal steps to get a basic Express server running with TypeScript.

Step 1: Initialize Your Project

First, create a new directory and initialize a Node.js project:

mkdir express-ts-server
cd express-ts-server
npm init -y

Step 2: Install Dependencies

Install the necessary packages:

npm install express
npm install --save-dev typescript @types/node @types/express ts-node nodemon

Here's what each package does:

  • express: The web framework
  • typescript: The TypeScript compiler
  • @types/node and @types/express: Type definitions
  • ts-node: Allows running TypeScript files directly
  • nodemon: Automatically restarts the server during development

Step 3: Create TypeScript Configuration

Create a tsconfig.json file in your project root:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Step 4: Create a Basic Folder Structure

Create a minimal project structure:

src/
└── index.ts    # Server entry point

Step 5: Create Your Express Server

Create the src/index.ts file:

import express, { Request, Response } from 'express';

// Initialize express app
const app = express();
const PORT = process.env.PORT || 3000;

// Middleware for parsing JSON
app.use(express.json());

// Basic route
app.get('/', (req: Request, res: Response) => {
  res.send('Express + TypeScript Server is running');
});

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

Step 6: Add npm Scripts

Update your package.json to include these scripts:

"scripts": {
  "start": "node dist/index.js",
  "dev": "nodemon src/index.ts",
  "build": "tsc"
}

Step 7: Run Your Server

Start the development server with:

npm run dev

Your server will now be running at http://localhost:3000, and it will automatically restart whenever you make changes to your code.

Adding a Simple API Endpoint

Let's add a simple API endpoint to demonstrate TypeScript with Express:

// Define a type for our data
interface User {
  id: number;
  name: string;
  email: string;
}

// Sample data
const users: User[] = [
  { id: 1, name: 'John Doe', email: 'john@example.com' },
  { id: 2, name: 'Jane Smith', email: 'jane@example.com' }
];

// Get all users endpoint
app.get('/api/users', (req: Request, res: Response) => {
  res.json(users);
});

// Get user by ID endpoint
app.get('/api/users/:id', (req: Request, res: Response) => {
  const id = parseInt(req.params.id);
  const user = users.find(u => u.id === id);
  
  if (!user) {
    return res.status(404).json({ message: 'User not found' });
  }
  
  res.json(user);
});

Building for Production

When you're ready to deploy your application, build it with:

npm run build

This will compile your TypeScript code to JavaScript in the dist directory. You can then run your production server with:

npm start

Conclusion

That's it! You now have a basic Express server set up with TypeScript. This configuration gives you the benefits of type safety while keeping the simplicity of Express.js. From here, you can expand your application by adding more routes, connecting to a database, or implementing authentication.

Not playing
Copyright © 2025