Let’s start by creating a simple Express server with an in-memory array to store users:
import express, { Request, Response } from "express";
import cors from "cors";
// Define the User type
interface User {
id: number;
name: string;
email: string;
}
// In-memory storage
const users: User[] = [
{ id: 1672531200000, name: "John Doe", email: "john@example.com" },
{ id: 1672531260000, name: "Jane Smith", email: "jane@example.com" },
];
const app = express();
const PORT = 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.get("/users", (req: Request, res: Response) => {
res.json(users);
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
This gives us a starting point with some existing users and a GET route to retrieve them.