forked from LeoHBK/react-postgres-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
40 lines (35 loc) · 963 Bytes
/
api.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
var dotenv = require("dotenv").config(),
express = require("express"),
pg = require("pg"),
cors = require("cors"),
app = express();
//Allowed cors in localhost
app.use(cors());
//Database Config .env
const config = {
user: process.env.PG_USER,
database: process.env.PG_DATABASE,
password: process.env.PG_PASS,
port: process.env.PG_PORT
};
//Documentation for node-postgres: https://node-postgres.com/
const pool = new pg.Pool(config);
app.get("/api.json", (req, res, next) => {
pool.connect(function(err, client, done) {
if (err) {
console.log("Can not connect to the DB because of " + err);
}
client.query("SELECT * FROM users", function(err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
res.status(200).send(result.rows);
});
});
});
//Server
app.listen(8080, function() {
console.log("API listening on http://localhost:8080/api.json");
});