Distansakademin

Express with TypeScript

Here’s a simple example of an Express app written using TypeScript. This example sets up a basic server with a single route that responds to HTTP GET requests:

First, make sure you have Node.js and npm (Node Package Manager) installed. Then, create a new directory for your project and follow these steps:

  1. Initialize your project and install the necessary packages:
mkdir express-ts-example
cd express-ts-example
npm init -y
npm install express @types/express typescript ts-node
  1. Create a TypeScript configuration file (tsconfig.json) in the project directory with the following content:
{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  }
}
  1. Create a new directory named “src” in the project directory.

  2. Inside the “src” directory, create a file named “app.ts” with the following content:

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

// Create an Express application
const app = express();

// Define a route that responds to GET requests
app.get('/', (req: Request, res: Response) => {
  res.send('Hello, Express with TypeScript!');
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});
  1. Open your terminal and navigate to the project directory.

  2. Run the TypeScript code using ts-node:

npx ts-node src/app.ts

Now your Express server should be up and running. If you open a web browser and navigate to http://localhost:3000, you should see the message “Hello, Express with TypeScript!” displayed.

Remember that this is just a simple example to get you started. You can expand this app by adding more routes, middleware, error handling, and more complex features as needed for your project.