Get Movie Details

node v8.12.0
version: 1.0.0
endpointsharetweet
const express = require('express'); const bodyParser = require('body-parser'); const http = require('http'); const API_KEY = 'f4c686b5'; const server = express(); server.use(bodyParser.urlencoded({ extended: true })); server.use(bodyParser.json()); server.post('/get-movie-details', (req, res) => { const movieToSearch = req.body.result && req.body.result.parameters && req.body.result.parameters.movie ? req.body.result.parameters.movie : 'The Godfather'; const reqUrl = encodeURI(`http://www.omdbapi.com/?t=${movieToSearch}&apikey=${API_KEY}`); http.get(reqUrl, (responseFromAPI) => { let completeResponse = ''; responseFromAPI.on('data', (chunk) => { completeResponse += chunk; }); responseFromAPI.on('end', () => { const movie = JSON.parse(completeResponse); let dataToSend = movieToSearch === 'The Godfather' ? `I don't have the required info on that. Here's some info on 'The Godfather' instead.\n` : ''; dataToSend += `${movie.Title} is a ${movie.Actors} starer ${movie.Genre} movie, released in ${movie.Year}. It was directed by ${movie.Director}`; return res.json({ speech: dataToSend, displayText: dataToSend, source: 'get-movie-details' }); }); }, (error) => { return res.json({ speech: 'Something went wrong!', displayText: 'Something went wrong!', source: 'get-movie-details' }); }); }); server.listen(3000, function () { console.log('Example app listening on port 3000!') }) process.env.RUNKIT_ENDPOINT_URL
Loading…

no comments

    sign in to comment