-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
138 lines (109 loc) · 3.08 KB
/
app.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require("express")
const bodyParser = require("body-parser")
const axios = require('axios')
const mongoose = require('mongoose');
require('dotenv').config()
const app = express()
const port = 3000
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
app.use(express.static(`${__dirname}/public`))
app.set("view engine", "ejs")
app.use(bodyParser.urlencoded({ extended: true }))
// Query Schema
const querySchema = mongoose.Schema({
query: {
type: String,
required: [true, "Query is required"]
},
filter: {
type: String,
required: [true, "Filter is required"]
},
queryOutput: {
type: Boolean,
required: [true, "Query result is required"]
},
updated: {
type: Date,
default: Date.now
}
})
// Query Model
const Query = mongoose.model("Query", querySchema)
var wordOutput = [];
var word = "";
var filter = "";
var queryResult;
app.get("/", (req, res) => {
res.render("index", {
title: "Word App"
})
})
app.get("/result", (req, res) => {
res.render("result", {
title: `'${word}' - '${filter}' - WordApp`,
item: wordOutput,
enteredWord: word,
query: filter
})
})
app.post("/result", (req, res) => {
word = req.body.word
filter = req.body.filter
var query = ""
checkBulkQuery = (entry) => {
let getAllWords = entry.split(" ")
let getLastWord = getAllWords[getAllWords.length - 1]
return getLastWord
}
switch (filter) {
case "synonyms":
query = `rel_syn=${checkBulkQuery(word)}`
break;
case "antonyms":
query = `rel_ant=${checkBulkQuery(word)}`
break;
case "rhymes":
query = `rel_rhy=${checkBulkQuery(word)}`
// query = `sp=${checkBulkQuery(word)}`
break;
case "related-words":
query = `ml=${word}`
break;
default:
break;
}
const url = `${process.env.QUERY_PREFFIX}${query}${process.env.QUERY_SUFFIX}`
axios.get(url)
.then(function (response) {
const wordData = response.data
wordOutput = wordData
wordOutput.length > 1
? queryResult = true
: queryResult = false
const saveQuery = Query({
query: word,
filter: filter,
queryOutput: queryResult,
updated: Date.now()
})
saveQuery.save((err) => {
if (err) console.log(err)
res.redirect("/result")
})
})
.catch(function (error) {
// handle error
console.log(error);
})
})
app.get("/track", (req, res) => {
Query.find({}, (err, documents) => {
if (err) console.log(err)
res.render("track", {
title: "Track Queries",
docs: documents
})
})
})
app.listen(process.env.PORT || port, () => console.log(`Server spinning on port ${port}`))