-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (115 loc) · 3.84 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
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
139
140
141
142
143
144
145
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
const db = require('./models');
const cryptoJS = require('crypto-js');
const postController = require('./controllers/posts');
const commentController = require('./controllers/comments');
const { Sequelize, Model, DataTypes } = require('sequelize');
const axios = require("axios");
// const userController = require('./controllers/users')
require('dotenv').config()
// MIDDLEWAREs
app.set('view engine', 'ejs')
app.use("/styles", express.static(__dirname + '/styles'));
app.use(cookieParser())
app.use(express.urlencoded({extended: false}))
// AUTHENTICATION MIDDLEWARE
app.use(async (req, res, next)=>{
if(req.cookies.userId) {
const decryptedId = cryptoJS.AES.decrypt(req.cookies.userId, process.env.SECRET)
const decryptedIdString = decryptedId.toString(cryptoJS.enc.Utf8)
const user = await db.user.findByPk(decryptedIdString)
res.locals.user = user
} else res.locals.user = null
next()
})
// CONTROLLERS
// ---------------- Posts ------------------
app.use('/users', require('./controllers/users'))
// app.use('/posts', require('./controllers/posts'))
// ROUTES
app.get('/', (req, res) => {
res.render('home', { user: res.locals.user })
})
// Create
app.post('/posts', postController.store)
// Read
app.get('/posts', postController.index)
app.get('/posts/:id', postController.show)
// Update
app.post('/posts/:id', postController.update)
// Delete
app.post('/deletePost/:id', postController.destroy)
// Display create form
app.get('/create', postController.create)
// Display create form
app.get('/createPost', postController.create)
// Display update form
app.get('/posts/:id/edit', postController.edit)
app.put('/posts/:id/edit', postController.edit)
// ------------ Comments -----------------
// Create
app.post('/comments', commentController.store)
// Read
app.get('/comments', commentController.index)
app.get('/comments/:id', commentController.show)
// Update
app.post('/comments/:id', commentController.update)
// Delete
app.post('/deleteComment/:id', commentController.destroy)
// Display create form
app.get('/create', commentController.create)
// Display create form
app.get('/createComment', commentController.create)
// Display update form
app.get('/comments/:id/edit', commentController.edit)
app.put('/comments/:id/edit', commentController.edit)
app.get('/search', async (req, res) => {
try {
const { query } = req.query;
const apiKey = process.env.API_KEY;
// Make API request to Giphy
const response = await axios.get(`https://api.giphy.com/v1/gifs/search`, {
params: {
api_key: apiKey,
q: query,
},
});
// Extract relevant data from API response
const gifs = response.data.data.map((gif) => {
return {
title: gif.title,
url: gif.images.fixed_height.url,
};
});
// Store the search results in the database
await db.gif.bulkCreate(gifs);
res.render('search', { gifs });
} catch (error) {
console.error(error);
console.log('error!! ', error)
res.status(500).send('Internal Service Error Processing Gif Search');
}
});
// Fetch trending GIFs from GIPHY API
// app.get('/gifs', async (req, res) => {
// try {
// const response = await fetch('https://api.giphy.com/v1/gifs/trending?api_key=UJZdaoJgztUnBDxgMi1rbL2rka5wdq9c&limit=10');
// const data = await response.json();
// const gifs = data.data.map(gif => {
// return {
// title: gif.title,
// url: gif.images.fixed_height.url
// };
// });
// res.render('gifs/create', { gifs });
// } catch (error) {
// console.error(error);
// res.status(500).send('Internal Server Error');
// }
// });
let port=process.env.PORT
app.listen(port || 8080, () => {
console.log('the server is running!')
})