youtube search

node v10.16.3
version: 2.0.0
endpointsharetweet
const express = require('express'); const http = require('http'); const https = require("https"); const app = express(); const port = 3000; getJSON = function(options, onResult) { console.log("rest::getJSON"); //var port = options.port == 443 ? https : http; var req = https.request(options, function(res) { var output = ''; console.log(options.host + ':' + res.statusCode); res.setEncoding('utf8'); res.on('data', function (chunk) { output += chunk; }); res.on('end', function() { var obj = JSON.parse(output); onResult(res.statusCode, obj); }); }); req.on('error', function(err) { res.send('error: ' + err.message); }); req.end(); }; app.get('/', (req, res) => { var query = req.query.q; var orsQuery = query.split(' ').join('%20'); var options = { host: 'www.googleapis.com', port: '', path: '/youtube/v3/search?part=snippet&maxResults=1&order=relevance&type=video&q=' + orsQuery + '&key=AIzaSyAD3ZxhOHg6sjyCoKjfz853wtHUhg7iIRk', method: 'GET', headers: { 'Content-Type': 'application/json' } }; getJSON(options, function(statusCode, result) { console.log("onResult: (" + statusCode + ")" + JSON.stringify(result)); res.statusCode = statusCode; res.send(result.items[0].id.videoId); }); }); app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Loading…

no comments

    sign in to comment