-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (41 loc) · 1.15 KB
/
index.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
import http from "node:http";
import mysql from "mysql2/promise";
import { faker } from "@faker-js/faker";
const hostname = "0.0.0.0";
const port = 3000;
const connection = await mysql.createConnection({
host: "fullcycle-mysql",
user: "root",
password: "root",
database: "fullcycle",
});
const server = http.createServer(async (req, res) => {
connection.connect();
let response = `
<h1>Fullcycle Rocks!</h1>
<h3>People:</h3>
<ul>
`;
try {
const name = faker.person.fullName();
await connection.query(
`INSERT INTO people (name) VALUES ('${name.replace(/'/g, "\\'")}')`
);
console.log(`Person name ${name} inserted!`);
} catch (error) {
console.error("Error:", error);
}
try {
const [results] = await connection.query("SELECT * FROM people");
results.forEach((person) => (response += `<li>${person.name}</li>`));
} catch (error) {
console.error("Error:", error);
}
response += "</ul>";
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
res.end(response);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});