javascript - How do I pass a req.setHeaders method on res.redirect method within the same app.get function? -
var express = require('express'); var app = express(); var port = process.env.port; app.get('/', function(req, res){ res.json('enter query parameters image search so: https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=cats&count=10 , latest search results enter url ttps://api.cognitive.microsoft.com/bing/v5.0/images/*'); });
here app.get method want parameters input user , redirect bing api return search results image search. big api requires api key passed header, ocp-apim-subscription-key: ••••••••••••••••••••••••••••••••. how do this?
app.get('/imagesearch/:image*', function(req, res){ console.log(req.params.image); console.log(req.query.count); res.redirect('https://api.cognitive.microsoft.com/bing/v5.0/images/search?q='+req.params.image+'&count='+req.query.count); }); app.listen(port, function(){ console.log('express listening on: '+port); });
since sounds want fetch data remote site , return response original request, can using request
module , piping response response original http request.
const request = require('request'); app.get('/imagesearch/:image*', function(req, res){ let options = { url: 'https://api.cognitive.microsoft.com/bing/v5.0/images/search?q='+req.params.image+'&count='+req.query.count, headers: {"ocp-apim-subscription-key": "xxxxx"} }; request(options).pipe(res); });
Comments
Post a Comment