-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (164 loc) · 3.95 KB
/
server.js
File metadata and controls
172 lines (164 loc) · 3.95 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
const express = require('express');
const { Pool } = require('pg');
const app = express();
const PORT = 3000;
// Database connection
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
// Middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Initialize database
async function initDatabase() {
try {
await pool.query(`
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
task TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Database initialized successfully');
} catch (err) {
console.error('Error initializing database:', err);
}
}
// HTML template
const getHTML = (todos) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo App</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 12px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
max-width: 500px;
width: 100%;
}
h1 {
color: #333;
margin-bottom: 20px;
text-align: center;
}
form {
display: flex;
gap: 10px;
margin-bottom: 30px;
}
input[type="text"] {
flex: 1;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type="text"]:focus {
outline: none;
border-color: #667eea;
}
button {
padding: 12px 24px;
background: #667eea;
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #5568d3;
}
.todos {
list-style: none;
}
.todo-item {
background: #f8f9fa;
padding: 15px;
margin-bottom: 10px;
border-radius: 6px;
border-left: 4px solid #667eea;
}
.todo-text {
color: #333;
font-size: 16px;
}
.todo-date {
color: #999;
font-size: 12px;
margin-top: 5px;
}
.empty {
text-align: center;
color: #999;
padding: 40px;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h1>📝 My Todos</h1>
<form action="/add" method="POST">
<input type="text" name="task" placeholder="What needs to be done?" required>
<button type="submit">Add</button>
</form>
<ul class="todos">
${todos.length === 0
? '<li class="empty">No todos yet. Add one above!</li>'
: todos.map(todo => `
<li class="todo-item">
<div class="todo-text">${todo.task}</div>
<div class="todo-date">${new Date(todo.created_at).toLocaleString()}</div>
</li>
`).join('')
}
</ul>
</div>
</body>
</html>
`;
// Routes
app.get('/', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM todos ORDER BY created_at DESC');
res.send(getHTML(result.rows));
} catch (err) {
console.error('Error fetching todos:', err);
res.status(500).send('Error loading todos');
}
});
app.post('/add', async (req, res) => {
const { task } = req.body;
try {
await pool.query('INSERT INTO todos (task) VALUES ($1)', [task]);
res.redirect('/');
} catch (err) {
console.error('Error adding todo:', err);
res.status(500).send('Error adding todo');
}
});
// Start server
initDatabase().then(() => {
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on http://localhost:${PORT}`);
});
});