-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (71 loc) · 2.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
- This is the index.js, main entry point of the app;
*/
/* ------ DO NOT CHANGE ANYTHING IN THIS FILE. ------ */
import express from "express";
import 'dotenv/config';
import cors from "cors";
// import { sqlite3, verbose } from "sqlite3";
import pkg from 'sqlite3';
import { sql } from "@vercel/postgres";
import router from "./routes/route.js";
import languages_array from "./languages.js";
const port = process.env.PORT || 3000;
const enviornment = process.env.NODE_ENV
const app = express();
const { sqlite3, verbose } = pkg;
const sqlite = verbose();
app.use(cors({
origin: "*",
methods: ['GET', 'POST']
}))
// app.use(cors())
app.get('/', (req, res) => {
if (enviornment === "development") {
res.send("Hello on LOCAL HOST");
}
if (enviornment === "production") {
res.redirect("https://akkshaytandon.github.io/farzi-vichar-api/")
}
});
app.get("/language-list", (req, res) => {
res.send(languages_array);
});
app.get("/get-user-quotes", (req, res) => {
async function readQuote() {
try {
const { rows } = await sql`SELECT * from user_quotes_table`;
// console.log(rows);
return res.status(200).json({ "message": "read successfully", "data": rows });
} catch (error) {
console.log(error);
return res.status(500).json({ "message": "attempt to read database failed" });
}
}
readQuote();
});
app.post("/add-user-quote", express.json(), (req, res) => {
const data = req.body;
// console.log(data)
async function addQuote() {
try {
await sql`CREATE TABLE IF NOT EXISTS user_quotes_table(
id SERIAL PRIMARY KEY,
author TEXT,
content TEXT UNIQUE,
UNIQUE (author, content)
);`;
await sql`INSERT INTO user_quotes_table(author, content) VALUES (${data.author}, ${data.content});`;
return res.status(200).json({ "message": "values inserted successfully" });
} catch (error) {
// console.log(error);
return res.status(500).json({ "message": "Error inserting values into the database", "error": error });
}
}
addQuote();
});
app.use("/language", router);
app.listen(port, () => {
console.log(`app running on http://localhost:${port}`);
});
export default app;