-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (30 loc) · 1.2 KB
/
Copy pathserver.js
File metadata and controls
33 lines (30 loc) · 1.2 KB
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
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const app = express();
const KEY = process.env.KEY;
const cors = require("cors");
const unescape = require("lodash.unescape");
app.use(cors());
/*
By default, the YouTube search API does not correctly format all HTML entities.
Instead, the HTML number shows up for things like apostrophes and internal quotation marks.
The easiest way to deal with this problem is to use the unescape function of the lodash library.
This axios request takes the response.data object and uses map to run unescape over the title of every YouTube video
in the search results before passing the data object to the React front end.
*/
app.get("/search", (req, res) => {
const url = `https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=50&key=${KEY}&q=${req.query.q}`;
axios
.get(url)
.then(response => response.data)
.then(data =>
data.items.map(item => {
item.snippet.title = unescape(item.snippet.title);
return item;
})
)
.then(data => res.send(data))
.catch(err => console.log(err));
});
app.listen(3001, console.log("server listening on port 3001"));