Skip to content

Commit 13248f9

Browse files
committed
use pg connection pool, init demo
1 parent f5d0cb0 commit 13248f9

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

client/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import Home from './containers/Home';
3-
import Test from './containers/Test';
3+
import Test from './containers/Demo';
44
import Navbar from './components/Navbar';
55
import { Switch, Route, BrowserRouter } from 'react-router-dom';
66
import './App.css';
@@ -12,7 +12,7 @@ function App() {
1212
<Navbar />
1313
<Switch>
1414
<Route exact path="/" component={Home} />
15-
<Route path="/test" component={Test} />
15+
<Route path="/demo" component={Test} />
1616
</Switch>
1717
</BrowserRouter>
1818
</div>

client/src/components/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Navbar = () => {
2020
className="navbar"
2121
>
2222
<Link to="/">Home</Link>
23-
<Link to="/test">Test React Router</Link>
23+
<Link to="/demo">Demo</Link>
2424
</div>
2525
);
2626
};

client/src/containers/Test.tsx renamed to client/src/containers/Demo.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import React, { useEffect, useState } from 'react';
22
import axios from 'axios';
33

44
const Test = () => {
5-
const [testMsg, setTestMsg] = useState<string>('');
6-
75
useEffect(() => {
86
(async () => {
9-
const res = await axios.get('/api/test');
10-
setTestMsg(res.data.msg);
7+
const res = await axios.get('/api/demo');
8+
console.log(res.data);
119
})();
1210
}, []);
1311

@@ -22,9 +20,7 @@ const Test = () => {
2220
fontSize: '2rem',
2321
fontWeight: 'bold'
2422
}}
25-
>
26-
{`${testMsg} ${process.env.REACT_APP_TEST_ENV}`}
27-
</div>
23+
></div>
2824
);
2925
};
3026

docker-compose-dev.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ services:
1515
- ./database/data:/var/lib/postgresql/data
1616
- ./database/initdb.sql:/docker-entrypoint-initdb.d/init.sql
1717
container_name: postgres_db
18+
##############
19+
## SERVER ##
1820
server:
1921
build:
2022
context: ./server
@@ -54,6 +56,7 @@ services:
5456
links:
5557
- server
5658
stdin_open: true
59+
restart: always
5760
container_name: client
5861
networks:
5962
webapp:

server/src/index.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,32 @@ app.use(express.json());
99
app.use(cors());
1010

1111
// postgres configuration
12-
const Client = pg.Client;
12+
const Pool = pg.Pool;
1313

14-
const client = new Client({
14+
const pool = new Pool({
1515
user: process.env.POSTGRES_USER,
1616
password: process.env.POSTGRES_PASSWORD,
1717
host: process.env.POSTGRES_HOST,
1818
database: process.env.POSTGRES_DB,
1919
port: 5432
2020
});
2121

22-
client
23-
.connect()
24-
.then(() => {
25-
console.log('connected succecfuly');
26-
})
27-
.then(() =>
28-
client.query('select Todo from Todolist', (err, res) => {
29-
if (err) console.log(err);
30-
console.log(res);
31-
})
32-
);
33-
3422
app.get('/api/start', (req, res) => {
3523
res.json({ msg: 'Start building your Postgres-Express-React Application' });
3624
});
3725

38-
app.get('/api/test', (req, res) => {
39-
res.json({ msg: 'This is a react-router test' });
26+
app.get('/api/demo', (req, res) => {
27+
pool
28+
.connect()
29+
.then((client) => {
30+
console.log('connected succecfuly');
31+
client.query('select Todo,Completed from Todolist', (err, queryRes) => {
32+
if (err) throw err;
33+
res.json({ data: queryRes?.rows });
34+
client.release();
35+
});
36+
})
37+
.catch((err) => console.log(err));
4038
});
4139

4240
const PORT = process.env.PORT || 5500;

0 commit comments

Comments
 (0)