{"size": 532, "ext": "js", "max_stars_repo_path": "server/image_search.js", "max_stars_repo_name": "mjhasbach/material-qna", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/image_search.js", "max_issues_repo_name": "mjhasbach/material-qna", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/image_search.js", "max_forks_repo_name": "mjhasbach/material-qna", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.1666666667, "max_line_length": 75, "alphanum_fraction": 0.484962406}
"use strict";

let config = require('./server_config'),
    Search = require('bing.search'),
    search = new Search(config.bing.accountKey);

module.exports = function(req, cb) {
    search.images(
        req.query.query,
        {
            top: 1,
            skip: req.query.index
        },
        function(err, images) {
            if (err) {
                return cb(err);
            }

            let image = images[0];

            image ? cb(null, image.url) : cb(new Error('No images found'));
        }
    );
};