forked from gitmohamed/WebSpeechDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (47 loc) · 1.41 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
const express = require('express')
const path = require('path')
const request = require('request')
const bodyParser = require('body-parser')
const YouTube = require('youtube-node')
const yt = new YouTube()
const config = require('./config')
const ytApi = config.key
const app = express()
app.set('port', (process.env.PORT || 3333))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.use(express.static('./dist'))
yt.setKey(ytApi)
app.get('/',(req, res) => {
res.sendFile(path.join(__dirname, './index.html'))
})
app.get('/2', (req, res) => {
res.sendFile(path.join(__dirname, './index2.html'))
})
var callYt = (q ,callback) => {
yt.search(q, 10, (err, result) => {
if (err) throw error
// yt.addParam('q', q);
var vids = [] // Array of video ids to send to client
for (var id in result.items) {
if (result.items.hasOwnProperty(id)) {
if (result.items[id].id.kind === 'youtube#video') {
vids.push(result.items[id].id.videoId)
}
}
}
callback(vids[0]) // return the videos as callback
})
}
app.get('/getVid', (req, res) => {
var vid = req.query.vid
callYt(vid, (data) => {
res.json(data) // send video id
})
})
app.get('/3', (req, res) => {
res.sendFile(path.join(__dirname, './index3.html'))
})
app.listen(app.get('port'), () => {
console.log(`Running app on localhost:${app.get('port')}...`)
})