From 2651dfe8581bf283b15df0d396c2cbd74377fcc9 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 29 Aug 2020 14:22:06 -0400 Subject: [PATCH] making index route use db --- controllers/people.js | 23 ++++------------------- postgres.js | 11 +++++++++++ server.js | 14 ++------------ 3 files changed, 17 insertions(+), 31 deletions(-) create mode 100644 postgres.js diff --git a/controllers/people.js b/controllers/people.js index a9ba1c0..c28c591 100644 --- a/controllers/people.js +++ b/controllers/people.js @@ -1,26 +1,11 @@ const express = require('express'); const router = express.Router(); - -let data = [ - { - id:1, - name:'Matt', - age: 38 - }, - { - id:2, - name:'Sally', - age: 54 - }, - { - id:3, - name:'Xanathar', - age: 17330 - }, -]; +const postgres = require('../postgres.js'); router.get('/', (req, res) => { - res.json(data) + postgres.query('SELECT * FROM people ORDER BY id ASC;', (err, results) => { + res.json(results.rows) + }); }); router.post('/', (req, res) => { diff --git a/postgres.js b/postgres.js new file mode 100644 index 0000000..3eff03e --- /dev/null +++ b/postgres.js @@ -0,0 +1,11 @@ +const Client = require('pg').Client + +const client = new Client({ + user: '', + host: 'localhost', + database: 'contacts', + password: '', + port: 5432, +}) + +module.exports = client; diff --git a/server.js b/server.js index 3b5c9fe..7f30810 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,6 @@ const express = require('express'); -const Client = require('pg').Client const app = express(); +const postgres = require('./postgres.js'); app.use(express.json()); app.use(express.static('public')) @@ -8,17 +8,7 @@ app.use(express.static('public')) const peopleController = require('./controllers/people.js'); app.use('/people', peopleController); -const client = new Client({ - user: '', - host: 'localhost', - database: 'contacts', - password: '', - port: 5432, -}) -client.connect(); -client.query('SELECT * FROM people;', (err, res) => { - console.log(res.rows) -}) +postgres.connect(); app.listen(3000, () => { console.log('listening');