-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
236 lines (204 loc) · 8.32 KB
/
server.js
File metadata and controls
236 lines (204 loc) · 8.32 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import express from 'express';
import mysql from 'mysql2';
import cors from 'cors';
import bodyParser from 'body-parser';
import bcrypt from 'bcrypt';
import path from 'path';
import { fileURLToPath } from 'url';
// --- 1. INITIALIZE APP ---
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3000;
console.log("1. App initialized...");
// --- 2. MIDDLEWARE ---
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// --- 3. DATABASE CONNECTION ---
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'skillswap'
});
db.connect(err => {
if (err) {
console.error('❌ Database connection failed: ' + err.stack);
return;
}
console.log('✅ Connected to MySQL database.');
});
// --- 4. CREATE TABLES ---
const createUsersTable = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
const createMessagesTable = `
CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
sender VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
db.query(createMessagesTable, (err) => { if(err) console.error("Messages table error:", err); });
const createConnectionsTable = `
CREATE TABLE IF NOT EXISTS connections (
id INT AUTO_INCREMENT PRIMARY KEY,
user_email VARCHAR(100) NOT NULL,
match_name VARCHAR(100) NOT NULL,
match_id INT NOT NULL,
status VARCHAR(20) DEFAULT 'connected',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
const createTransactionsTable = `
CREATE TABLE IF NOT EXISTS transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
user_email VARCHAR(100) NOT NULL,
type VARCHAR(20) NOT NULL,
description VARCHAR(255) NOT NULL,
partner VARCHAR(100) DEFAULT 'System',
amount INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
db.query(createUsersTable, (err) => { if(err) console.error("Users table error:", err); });
db.query(createConnectionsTable, (err) => { if(err) console.error("Connections table error:", err); });
db.query(createTransactionsTable, (err) => {
if(err) console.error("Transactions table error:", err);
else console.log("✅ All tables ready.");
});
// --- 5. ROUTES ---
// REGISTER (With Server-Side Regex Validation)
app.post('/register', async (req, res) => {
const { full_name, email, password } = req.body;
// Regex Patterns
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const passwordRegex = /^.{6,}$/; // Min 6 chars
// Validation Checks
if (!full_name || !email || !password) {
return res.status(400).json({ status: 'error', message: 'All fields required' });
}
if (!emailRegex.test(email)) {
return res.status(400).json({ status: 'error', message: 'Invalid email format' });
}
if (!passwordRegex.test(password)) {
return res.status(400).json({ status: 'error', message: 'Password must be 6+ chars' });
}
try {
const hashedPassword = await bcrypt.hash(password, 10);
const insertQuery = 'INSERT INTO users (full_name, email, password) VALUES (?, ?, ?)';
db.query(insertQuery, [full_name, email, hashedPassword], (err) => {
if (err) {
if (err.code === 'ER_DUP_ENTRY') return res.status(409).json({ status: 'error', message: 'User already exists' });
return res.status(500).json({ status: 'error', message: 'Database error' });
}
res.status(201).json({ status: 'success', message: 'Registered successfully' });
});
} catch (error) {
res.status(500).json({ status: 'error', message: 'Server error' });
}
});
// --- CHAT ROUTES (POLLING) ---
// 1. GET MESSAGES (The "Poll")
app.get('/messages', (req, res) => {
// Get all messages ordered by time
db.query('SELECT * FROM messages ORDER BY created_at ASC', (err, results) => {
if (err) return res.status(500).json({ error: 'Database error' });
res.json(results);
});
});
// 2. SEND MESSAGE
app.post('/messages', (req, res) => {
const { sender, content } = req.body;
if (!content) return res.status(400).json({ error: 'No content' });
const sql = 'INSERT INTO messages (sender, content) VALUES (?, ?)';
db.query(sql, [sender, content], (err) => {
if (err) return res.status(500).json({ error: 'Database error' });
res.json({ status: 'success' });
});
});
// LOGIN
app.post('/login', async (req, res) => {
const { email, password } = req.body;
if (!email || !password) return res.status(400).json({ status: 'error', message: 'Fields required' });
const sql = 'SELECT * FROM users WHERE email = ?';
db.query(sql, [email], async (err, results) => {
if (err) return res.status(500).json({ status: 'error', message: 'Database error' });
if (results.length === 0) return res.status(401).json({ status: 'error', message: 'User not found' });
const user = results[0];
const match = await bcrypt.compare(password, user.password);
if (match) {
const { password, ...userSafe } = user;
res.json({ status: 'success', message: 'Login successful', user: userSafe });
} else {
res.status(401).json({ status: 'error', message: 'Invalid password' });
}
});
});
// CONNECT
app.post('/connect', (req, res) => {
const { user_email, match_name, match_id } = req.body;
const checkSql = 'SELECT * FROM connections WHERE user_email = ? AND match_id = ?';
db.query(checkSql, [user_email, match_id], (err, results) => {
if (err) return res.status(500).json({ status: 'error' });
if (results.length > 0) return res.json({ status: 'success', message: 'Already connected' });
const insertSql = 'INSERT INTO connections (user_email, match_name, match_id) VALUES (?, ?, ?)';
db.query(insertSql, [user_email, match_name, match_id], (err) => {
if (err) return res.status(500).json({ status: 'error' });
res.json({ status: 'success', message: 'Connected!' });
});
});
});
app.get('/my-connections', (req, res) => {
const { email } = req.query;
const sql = 'SELECT * FROM connections WHERE user_email = ?';
db.query(sql, [email], (err, results) => {
if (err) return res.status(500).json({ status: 'error' });
res.json({ status: 'success', connections: results });
});
});
// GET TRANSACTIONS
app.get('/my-transactions', (req, res) => {
const { email } = req.query;
// 1. Check if user has any transactions
const checkSql = 'SELECT * FROM transactions WHERE user_email = ? ORDER BY created_at DESC';
db.query(checkSql, [email], (err, results) => {
if (err) return res.status(500).json({ status: 'error' });
// 2. If NO transactions, insert a "Welcome Bonus" automatically
if (results.length === 0) {
const insertSql = `INSERT INTO transactions (user_email, type, description, partner, amount)
VALUES (?, 'bonus', 'Welcome Bonus', 'SkillSwap', 150)`;
db.query(insertSql, [email], (err) => {
if (err) return res.status(500).json({ status: 'error' });
// Return the new fake transaction immediately
return res.json({
status: 'success',
transactions: [{
type: 'bonus',
description: 'Welcome Bonus',
partner: 'SkillSwap',
amount: 150,
created_at: new Date()
}]
});
});
} else {
// 3. Return real transactions
res.json({ status: 'success', transactions: results });
}
});
});
// --- 6. START SERVER ---
console.log("Attempting to start server...");
app.listen(PORT, () => {
console.log(`\n🚀 Server running on http://localhost:${PORT}`);
console.log(" (Press Ctrl + C to stop)\n");
});