-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (60 loc) · 2.14 KB
/
Copy pathserver.js
File metadata and controls
76 lines (60 loc) · 2.14 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
import express from 'express';
import { createClient } from '@libsql/client';
import path from 'path';
import url from 'url';
// Get the directory name in ES Modules
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// having issues w/ .env file, made manual const for time being (careful not to push!!)
const TURSO_CONNECTION_URL="your-turso-url"
const TURSO_AUTH_TOKEN="your-turso-token"
const turso = createClient({
url: TURSO_CONNECTION_URL,
authToken: TURSO_AUTH_TOKEN,
});
const app = express();
const port = 3000; // change if you have to
app.use(express.json());
// run npm run build first
app.use(express.static(path.join(__dirname, 'build')));
// GET requests to root URL (signin)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.post('/signin', async (req, res) => {
console.log('Received POST request'); // debugging
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json({ error: 'Email and password are required.' });
}
try {
await turso.execute({
sql: "INSERT INTO users (email, password) VALUES (?, ?)",
args: [email, password],
});
res.status(200).json({ success: true });
} catch (error) {
console.error('Error inserting data:', error);
res.status(500).json({ error: 'Failed to sign up' });
}
});
app.post('/checkout', async (req, res) => {
const { firstName, lastName, cardNum, cardCVV, cardExpr } = req.body;
if (!firstName || !lastName || !cardNum || !cardCVV || !cardExpr) {
return res.status(400).json({ error: 'All fields are required.' });
}
try {
await turso.execute({
sql: ` INSERT INTO cardInfo (firstName, lastName, cardNum, cardCVV, cardExpr) VALUES (?, ?, ?, ?, ?)
`,
args: [firstName, lastName, cardNum, cardCVV, cardExpr],
});
res.status(200).json({ success: true });
} catch (error) {
console.error('Error saving payment:', error);
res.status(500).json({ error: 'Failed to save payment info.' });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});