Spaces:
Building
Building
File size: 775 Bytes
c6be938 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import prisma from "./PrismaClient.js";
// Load environment variables
dotenv.config();
// App config
const app = express();
const port = process.env.PORT || 3010;
// Middleware
app.use(express.json());
app.use(cors());
app.get("/", (req, res) => {
res.send("Hello World!");
});
// Routes
app.get("/api/users", async (req, res) => {
try {
const users = await prisma.user.findMany();
res.json(users);
} catch (error) {
console.error("Error fetching users:", error);
res.status(500).json({ error: "An error occurred while fetching users." });
}
});
// Start server
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
export default app; |