Repository navigation
Expand file tree
/
Copy pathexample.ts
More file actions
94 lines (81 loc) · 1.81 KB
/
Copy pathexample.ts
File metadata and controls
94 lines (81 loc) · 1.81 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bun
import { cors, createApp, json, logger } from "./index.ts";
// Create app with configuration
const app = createApp({
port: 3000,
development: true,
cors: {
origin: true,
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
},
});
// Global middleware
app.use(logger());
app.use(cors());
app.use(json());
// Basic routes
app.get("/", (ctx) => {
return ctx.json({
message: "Welcome to NinoTS!",
framework: "NinoTS",
runtime: "Bun",
timestamp: new Date().toISOString(),
});
});
app.get("/health", (ctx) => {
return ctx.json({
status: "healthy",
uptime: process.uptime(),
memory: process.memoryUsage(),
});
});
// Route with parameters
app.get("/users/:id", (ctx) => {
const { id } = ctx.params;
return ctx.json({
user: {
id,
name: `User ${id}`,
email: `user${id}@example.com`,
},
});
});
// Route with query parameters
app.get("/search", (ctx) => {
const { q, limit = "10" } = ctx.query;
return ctx.json({
query: q,
limit: parseInt(limit),
results: q ? [`Result for "${q}"`] : [],
});
});
// POST route with body
app.post("/users", (ctx) => {
const userData = ctx.body as Record<string, unknown>;
return ctx.status(201).json({
message: "User created",
user: {
id: Math.random().toString(36).substr(2, 9),
...userData,
createdAt: new Date().toISOString(),
},
});
});
// Error route for testing
app.get("/error", () => {
throw new Error("This is a test error");
});
// 404 handling is automatic
// Error handling
app.onError((error, ctx) => {
console.error("Application error:", error.message);
return ctx.status(500).json({
error: "Internal Server Error",
message: error.message,
stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
});
});
// Start server
console.log("🚀 Starting NinoTS server...");
app.listen();