-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (83 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
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const { Pool } = require('pg');
// Set up body-parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Enable CORS
app.use(cors());
// Create a PostgreSQL connection pool
const pool = new Pool({
user: 'njokoth',
host: 'localhost',
database: 'mangomdb',
password: 'thisisforresearch',
port: 5432,
});
// Define a POST route for creating an account
app.post('/create-account', async (req, res) => {
const { fullName, username, password } = req.body;
try {
// Insert the user data into the database
await pool.query('INSERT INTO users (full_name, username, password) VALUES ($1, $2, $3)', [fullName, username, password]);
res.sendStatus(200);
} catch (error) {
console.error('Error creating account:', error);
res.sendStatus(500);
}
});
// Define a POST route for login
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
// Query the database to check if the credentials match
const result = await pool.query('SELECT * FROM users WHERE username = $1 AND password = $2', [username, password]);
const user = result.rows[0];
if (user) {
res.sendStatus(200);
} else {
res.sendStatus(401);
}
} catch (error) {
console.error('Error during login:', error);
res.sendStatus(500);
}
});
// Define a POST route for sending a message
app.post('/send-message', async (req, res) => {
const { senderUsername, recipientUsername, messageText } = req.body;
try {
// Check if the recipient's username exists in the users table
const recipientExists = await pool.query('SELECT * FROM users WHERE username = $1', [recipientUsername]);
if (recipientExists.rows.length === 0) {
res.status(400).json({ error: 'Recipient username not found' });
return;
}
// Insert the message data into the messages table, including the timestamp
const timestamp = new Date().toISOString(); // Use ISO 8601 format for the timestamp
await pool.query('INSERT INTO messages (sender_username, recipient_username, message_text, timestamp) VALUES ($1, $2, $3, $4)', [senderUsername, recipientUsername, messageText, timestamp]);
res.sendStatus(200);
} catch (error) {
console.error('Error sending message:', error);
res.sendStatus(500);
}
});
// Define a GET route for fetching user messages
app.get('/get-messages', async (req, res) => {
const recipientUsername = req.query.recipientUsername;
try {
// Fetch messages for the recipient username from the messages table
const result = await pool.query('SELECT * FROM messages WHERE recipient_username = $1', [recipientUsername]);
const messages = result.rows;
res.json(messages);
} catch (error) {
console.error('Error fetching messages:', error);
res.sendStatus(500);
}
});
// Start the server
app.listen(3000, () => {
console.log('Server running on port 3000');
});