forked from hnasr/javascript_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (33 loc) · 976 Bytes
/
index.js
File metadata and controls
43 lines (33 loc) · 976 Bytes
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
const app = require('express')();
const fs = require('fs');
const pg = require( 'pg');
const pool = new pg.Pool({
"host": "husseinmac.local",
"port": 5433,
"user":"postgres",
"password" : "password",
"database" : "postgres",
"max": 20,
"connectionTimeoutMillis" : 0,
"idleTimeoutMillis": 0
})
app.get("/", (req,res) => {
res.sendFile(__dirname + "/index.html")
})
let id = 0;
app.post("/", async (req,res) => {
try {
const txtIndex = fs.readFileSync(__dirname + "/index.html")
const sql = "insert into orders (username) values ($1)";
const result = await pool.query(sql, ['hussein']);
id++;
const updatedIndexHtml = txtIndex.toString().replace("<!-- ORDER -->", `<h1>Order ${id} placed successfully</h1>`)
//res.headers.add("")
res.send(updatedIndexHtml)
}
catch(ex){
console.error(ex)
res.send(ex)
}
})
app.listen(8080);