-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (55 loc) · 1.57 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express')
const dotenv = require("dotenv");
const pg = require("pg");
dotenv.config();
const { Pool } = pg;
const pool = new Pool({
connectionString: process.env.POSTGRES_URL,
});
const app = express();
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,CONNECT,TRACE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, X-Content-Type-Options, Accept, X-Requested-With, Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
);
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Private-Network", true);
res.setHeader("Access-Control-Max-Age", 7200);
next();
});
app.get("/api/search", (req, res) => {
const { query } = req.query;
pool
.query(
`SELECT * FROM posts
WHERE "postTitle" ILIKE $1 OR "postText" ILIKE $1;`,
[`%${query}%`]
)
.then((data) => res.json(data.rows))
.catch((e) => {
console.log("Error:", e);
res.sendStatus(500);
});
});
app.get("/api/post/:id", (req, res) => {
const { id } = req.params;
pool
.query(`SELECT * FROM posts WHERE "postID"=$1`, [id])
.then((data) => res.json(data.rows))
.catch((e) => res.sendStatus(404));
});
app.get("/api", (req, res) =>
pool
.query("SELECT * FROM posts")
.then((data) => res.json(data.rows))
.catch((e) => res.sendStatus(500))
);
const server = app.listen(8585, () => {
console.log("Server running on Port 8585.");
});
module.exports = app