-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (92 loc) · 3.28 KB
/
server.js
File metadata and controls
109 lines (92 loc) · 3.28 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable @typescript-eslint/no-require-imports */
// Load environment variables
require("dotenv").config({ path: ".env.local" });
const express = require("express");
const cors = require("cors");
const { handler } = require("./local-lambda");
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
// Mock API Gateway event converter
const convertToAPIGatewayEvent = (req) => {
const { method, path, body, headers, query } = req;
// Convert Express route to API Gateway route key format
let routeKey; //
const pathParameters = {}; //
const idMatch = path.match(/\/(\d+)$/); //
if (method === "GET" && path === "/api/books") {
routeKey = "GET /books";
} else if (method === "GET" && path.match(/^\/api\/books\/\d+$/)) {
routeKey = "GET /books/{id}";
} else if (method === "PUT" && path === "/api/books") {
routeKey = "PUT /books";
} else if (method === "DELETE" && path.match(/^\/api\/books\/\d+$/)) {
routeKey = "DELETE /books/{id}";
} else if (method === "POST" && path === "/api/register") {
routeKey = "POST /register";
} else if (method === "POST" && path === "/api/login") {
routeKey = "POST /login";
} else if (method === "DELETE" && path === "/api/account") {
routeKey = "DELETE /account";
} else if (method === "GET" && path === "/api/my-books") {
routeKey = "GET /my-books";
}
if (idMatch) {
pathParameters.id = idMatch[1];
}
return {
routeKey,
pathParameters,
queryStringParameters: query,
headers,
body: body ? JSON.stringify(body) : null,
};
};
// Routes
app.all("/api/*", async (req, res) => {
try {
// Convert Express request to API Gateway event
const event = convertToAPIGatewayEvent(req);
if (!event.routeKey) {
return res.status(404).json({ error: "Route not found" });
}
// Call the lambda handler
const result = await handler(event);
// Send response
res.status(result.statusCode);
// Set headers if any
if (result.headers) {
Object.entries(result.headers).forEach(([key, value]) => {
res.set(key, value);
});
}
// Parse and send body
const responseBody = result.body ? JSON.parse(result.body) : null;
res.json(responseBody);
} catch (error) {
console.error("Server error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
// Health check
app.get("/health", (_req, res) => {
res.json({ status: "OK", timestamp: new Date().toISOString() });
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 Local API server running on http://localhost:${PORT}`);
console.log(`📝 API endpoints:`);
console.log(` GET http://localhost:${PORT}/api/books`);
console.log(` GET http://localhost:${PORT}/api/books/{id}`);
console.log(` PUT http://localhost:${PORT}/api/books`);
console.log(` DELETE http://localhost:${PORT}/api/books/{id}`);
console.log(` POST http://localhost:${PORT}/api/register`);
console.log(` POST http://localhost:${PORT}/api/login`);
console.log(` GET http://localhost:${PORT}/api/my-books`);
console.log(`🏥 Health check: http://localhost:${PORT}/health`);
console.log(` DELETE http://localhost:${PORT}/api/account`);
console.log();
});
module.exports = app;