-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
212 lines (189 loc) · 5.73 KB
/
server.mjs
File metadata and controls
212 lines (189 loc) · 5.73 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// server.mjs - Using ES modules syntax and built-in Node.js modules
import http from 'http';
import { URL } from 'url';
import fs from 'fs';
import path from 'path';
// In-memory database for development (since we can't connect to MongoDB)
const db = {
users: [],
products: [],
orders: []
};
// Load mock data if available
try {
const mockDataPath = './src/lib/mock-data.ts';
if (fs.existsSync(mockDataPath)) {
console.log('Loading mock data...');
// This is just a placeholder - we can't actually import TS files directly
// In a real scenario, you'd convert the mock data to JSON
}
} catch (error) {
console.error('Error loading mock data:', error);
}
// CORS headers
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '86400'
};
// Parse request body
const parseBody = async (req) => {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
resolve(body ? JSON.parse(body) : {});
} catch (error) {
reject(error);
}
});
req.on('error', reject);
});
};
// Send JSON response
const sendJSON = (res, data, status = 200) => {
res.writeHead(status, {
'Content-Type': 'application/json',
...corsHeaders
});
res.end(JSON.stringify(data));
};
// Handle API routes
const handleRequest = async (req, res) => {
// Handle CORS preflight requests
if (req.method === 'OPTIONS') {
res.writeHead(204, corsHeaders);
res.end();
return;
}
// Parse URL
const baseURL = `http://${req.headers.host}`;
const url = new URL(req.url, baseURL);
const pathname = url.pathname;
console.log(`${req.method} ${pathname}`);
// Health check
if (pathname === '/api/health') {
return sendJSON(res, { status: 'ok', message: 'Server is running' });
}
// Users API
if (pathname === '/api/users' && req.method === 'GET') {
return sendJSON(res, db.users);
}
if (pathname === '/api/users' && req.method === 'POST') {
try {
const userData = await parseBody(req);
const newUser = {
_id: `user_${Date.now()}`,
...userData,
createdAt: new Date(),
updatedAt: new Date()
};
db.users.push(newUser);
return sendJSON(res, newUser, 201);
} catch (error) {
return sendJSON(res, { error: error.message }, 500);
}
}
if (pathname.startsWith('/api/users/') && req.method === 'GET') {
const id = pathname.split('/').pop();
const user = db.users.find(u => u._id === id);
if (!user) {
return sendJSON(res, { error: 'User not found' }, 404);
}
return sendJSON(res, user);
}
// Products API
if (pathname === '/api/products' && req.method === 'GET') {
return sendJSON(res, db.products);
}
if (pathname === '/api/products' && req.method === 'POST') {
try {
const productData = await parseBody(req);
const newProduct = {
_id: `product_${Date.now()}`,
...productData,
createdAt: new Date(),
updatedAt: new Date()
};
db.products.push(newProduct);
return sendJSON(res, newProduct, 201);
} catch (error) {
return sendJSON(res, { error: error.message }, 500);
}
}
if (pathname.startsWith('/api/products/') && req.method === 'GET') {
const id = pathname.split('/').pop();
const product = db.products.find(p => p._id === id);
if (!product) {
return sendJSON(res, { error: 'Product not found' }, 404);
}
return sendJSON(res, product);
}
// Orders API
if (pathname === '/api/orders' && req.method === 'GET') {
return sendJSON(res, db.orders);
}
if (pathname === '/api/orders' && req.method === 'POST') {
try {
const orderData = await parseBody(req);
const newOrder = {
_id: `order_${Date.now()}`,
...orderData,
createdAt: new Date(),
updatedAt: new Date()
};
db.orders.push(newOrder);
return sendJSON(res, newOrder, 201);
} catch (error) {
return sendJSON(res, { error: error.message }, 500);
}
}
if (pathname.startsWith('/api/orders/') && req.method === 'GET') {
const id = pathname.split('/').pop();
const order = db.orders.find(o => o._id === id);
if (!order) {
return sendJSON(res, { error: 'Order not found' }, 404);
}
return sendJSON(res, order);
}
// Stripe payment API
if (pathname === '/api/payment/create-intent' && req.method === 'POST') {
try {
const { amount, currency } = await parseBody(req);
const paymentIntent = {
id: `pi_${Math.random().toString(36).substring(2, 15)}`,
client_secret: `pi_${Math.random().toString(36).substring(2, 15)}_secret_${Math.random().toString(36).substring(2, 15)}`,
amount,
currency,
status: 'requires_payment_method'
};
return sendJSON(res, paymentIntent);
} catch (error) {
return sendJSON(res, { error: error.message }, 500);
}
}
if (pathname === '/api/payment/success' && req.method === 'POST') {
try {
const { paymentIntentId } = await parseBody(req);
return sendJSON(res, {
id: paymentIntentId,
status: 'succeeded'
});
} catch (error) {
return sendJSON(res, { error: error.message }, 500);
}
}
// Not found
sendJSON(res, { error: 'Not found' }, 404);
};
// Create and start server
const PORT = 5000;
const server = http.createServer(handleRequest);
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
console.log(`API available at http://localhost:${PORT}/api/health`);
});