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:
mkdir express-ts-example
cd express-ts-example
npm init -y
npm install express @types/express typescript ts-node
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
}
}
Create a new directory named “src” in the project directory.
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}`);
});
Open your terminal and navigate to the project directory.
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.