-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
115 lines (100 loc) · 3.02 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
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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Store addresses (in a real app, this would be a database)
let addresses = [];
// GET endpoint to retrieve all addresses
app.get('/addresses', (req, res) => {
try {
res.status(200).json({
success: true,
data: addresses
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// NEW: GET endpoint to retrieve account address by email
app.get('/address', (req, res) => {
try {
const { email } = req.query;
const emailAddress = email;
console.log("fetching account address for emailAddress",emailAddress);
if (!emailAddress) {
return res.status(400).json({
success: false,
error: 'Please provide an emailAddress in the request body'
});
}
const address = addresses.find(addr => addr.emailAddress === emailAddress);
if (!address) {
return res.status(404).json({
success: false,
error: 'No account found for this email address'
});
}
res.status(200).json({
success: true,
data: {
accountAddress: address.accountAddress,
emailAddress: address.emailAddress
}
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// POST endpoint to add new address
app.post('/address', (req, res) => {
try {
const { accountAddress, emailAddress } = req.body;
console.log("adding account address for emailAddress",emailAddress);
// Validate input
if (!accountAddress || !emailAddress) {
return res.status(400).json({
success: false,
error: 'Please provide both accountAddress and emailAddress'
});
}
// Check if email already exists
const existingAddress = addresses.find(addr => addr.emailAddress === emailAddress);
if (existingAddress) {
return res.status(400).json({
success: false,
error: 'Email address already registered'
});
}
// Add new address
const newAddress = {
accountAddress,
emailAddress,
timestamp: new Date().toISOString()
};
addresses.push(newAddress);
res.status(201).json({
success: true,
data: newAddress
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});