-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (113 loc) · 3.08 KB
/
server.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
const express = require('express')
const MongoClient = require('mongodb').MongoClient
const app = express()
querystring = require('querystring')
url = require('url')
require('dotenv').config()
let db, memeCollection
let dbConnectionStr = process.env.DB_STRING
let dbName = 'bts-memes'
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
.then(client => {
console.log(`Connected to ${dbName} database.`)
db = client.db(dbName)
memeCollection = db.collection('memes')
})
// Middleware
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// Read (Get)
app.get('/api', (req, res) => {
memeCollection.find().toArray()
.then(data => {
res.render('api.ejs', { memeData: data })
})
.catch(err => console.error(err))
})
// Search
app.get('/', (req, res) => {
const queryObj = url.parse(req.url, true).query
const query = JSON.parse(JSON.stringify(queryObj))
if (query.member === "All") {delete query.member}
if (query.category === "All") {delete query.category}
memeCollection.find(
{...query},
{ collation: // case insensitive index
{
locale: 'en',
strength: 2
}
}
).toArray()
.then(data => {
res.render('index.ejs', { memeData: data })
})
.catch(err => console.error(err))
})
// PUBLIC API ROUTES --------------------------------
// Return all memes
app.get('/memes', (req, res) => {
memeCollection.find().toArray()
.then(data => {
res.status(200).json({"member": data[0].member,
"category": data[0].category,
"description": data[0].description,
"imageUrl": data[0].imageUrl})
})
.catch(err => console.error(err))
})
// Return array of memes by member
app.get('/memes/member/:member', (req, res) => {
memeCollection.find(
{"member": req.params.member},
{ collation: // case insensitive index
{
locale: 'en',
strength: 2
}
}
).toArray()
.then(data => {
res.status(200).json({"member": data[0].member,
"category": data[0].category,
"description": data[0].description,
"imageUrl": data[0].imageUrl})
})
.catch(err => console.error(err))
})
// Return array of memes by category
app.get('/memes/category/:category', (req, res) => {
memeCollection.find(
{"category": req.params.category},
{ collation: // case insensitive index
{
locale: 'en',
strength: 2
}
}
).toArray()
.then(data => {
res.status(200).json({"member": data[0].member,
"category": data[0].category,
"description": data[0].description,
"imageUrl": data[0].imageUrl})
})
.catch(err => console.error(err))
})
// Return a random meme
app.get('/memes/random', (req, res) => {
memeCollection.aggregate([{ $sample: { size: 1 } }])
.toArray()
.then(data => {
res.status(200).json({"member": data[0].member,
"category": data[0].category,
"description": data[0].description,
"imageUrl": data[0].imageUrl})
})
.catch(err => console.error(err))
})
app.listen(process.env.PORT || 3000, () => {
console.log(`Server running.`)
})