简体   繁体   中英

Get key value pair from API call with Express and Request module

I'm able to query an timetable API and return the full JSON response, using the request module. My end goal is to render the times using pug.

var express = require('express');
var router = express.Router();
var request = require('request');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Salah' });
});

/* GET time page. */
router.get('/time', function(req, res, next) {
  var test = request({
    url: 'http://api.aladhan.com/timingsByCity',
    qs: {
      school: '1',
      country: 'uk',
      city: 'london'
    }
  }).timings.Sunrise.pipe(res);
  // }).pipe(res);
});

module.exports = router;

My current route localhost:3000/time renders the same as this . I'd like it to just return some specific timings, but I've had no luck using .timings.Sunrise.pipe(res)

You may do something like:

router.get('/time', function(req, res) {
  request({
    url: 'http://api.aladhan.com/timingsByCity',
    qs: {
      school: '1',
      country: 'uk',
      city: 'london'
    }
  }, function(error, request, body) {
    var info = JSON.parse(body);
    res.render('showTimings', { timings: info.data.timings });
  });
});

showTimings.pug

ul
 for val, key in timings
   li= key + ': ' + val

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM