-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
170 lines (157 loc) · 4.55 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
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
const express = require('express');
const { Pool } = require('pg');
const app = express();
const pool = new Pool({
connectionString: process.env.DATABASE_URL, // PostgreSQL connection string from environment variable
});
app.use(express.json()); // Middleware to parse JSON bodies
app.use(express.urlencoded({ extended: true })); // Middleware to parse URL-encoded bodies
// Define a root path route handler
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node.js PostgreSQL Car API</title>
</head>
<body>
<h1>Welcome to the Node.js PostgreSQL Car API!</h1>
<ul>
<li><a href="/cars">View Cars</a> - GET /cars</li>
<li><a href="/add-car">Add a Car</a> - GET /add-car (to see the form)</li>
<li><a href="/health">Health Check</a> - GET /health</li>
</ul>
</body>
</html>
`);
});
// Serve an HTML form at the /add-car route for GET requests
app.get('/add-car', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Car</title>
</head>
<body>
<h1>Add a New Car</h1>
<form action="/add-car" method="post">
<label for="name">Car Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="number">Number:</label>
<input type="number" id="number" name="number" required><br><br>
<button type="submit">Add Car</button>
</form>
<a href="/">Back to Home</a>
</body>
</html>
`);
});
// Route to add a car with POST request
app.post('/add-car', async (req, res) => {
console.log(req.body); // Log request body to inspect the data
const { name, number } = req.body;
try {
await pool.query('INSERT INTO cars(name, number) VALUES($1, $2)', [name, number]);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Car Added</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.message { color: green; }
</style>
</head>
<body>
<div class="message">Car added successfully!</div>
<a href="/add-car">Add another car</a> | <a href="/cars">View Cars</a>
</body>
</html>
`);
} catch (err) {
console.error(err);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Error Adding Car</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.error { color: red; }
</style>
</head>
<body>
<div class="error">Error adding the car to the database. Please try again.</div>
<a href="/add-car">Go back</a>
</body>
</html>
`);
}
});
// Route to get all cars with GET request
app.get('/cars', async (req, res) => {
try {
const { rows } = await pool.query('SELECT * FROM cars ORDER BY id ASC');
let html = `
<!DOCTYPE html>
<html>
<head>
<title>Cars List</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
</style>
</head>
<body>
<h1>List of Cars</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Number</th>
</tr>`;
rows.forEach(car => {
html += `<tr>
<td>${car.id}</td>
<td>${car.name}</td>
<td>${car.number}</td>
</tr>`;
});
html += `</table>
<a href="/add-car">Add a New Car</a>
</body>
</html>`;
res.send(html);
} catch (err) {
console.error(err);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.error { color: red; }
</style>
</head>
<body>
<div class="error">Error retrieving cars from the database. Please try again.</div>
<a href="/cars">Try Again</a>
</body>
</html>
`);
}
});
// Health check route
app.get('/health', (req, res) => res.send('OK'));
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});