-
-
Save alexbeletsky/a3ce02a05790e24a3176 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var google = require('google'); | |
| var app = express(); | |
| function googleSearch(opts, callback) { | |
| var search = config(opts); | |
| var results = [], pages = 0; | |
| search(opts.query, function (err, next, links) { | |
| if (err) { | |
| return callback(err); | |
| } | |
| links = links.map(function (link) { | |
| return {title: link.title, link: link.link, description: link.description}; | |
| }); | |
| results = results.concat(links); | |
| if (pages < opts.nextPages) { | |
| pages =+ 1; | |
| next(); | |
| } else { | |
| callback(null, {query: opts.query, results: results}); | |
| } | |
| }); | |
| function config(opts) { | |
| google.resultsPerPage = opts.resultsPerPage || 25; | |
| google.tld = opts.tld; | |
| google.lang = opts.lang; | |
| return google; | |
| } | |
| } | |
| app.get('/search', function (req, res, next) { | |
| var query = req.query.query; | |
| var lang = req.query.lang || 'en'; | |
| var tld = req.query.tld || 'de'; | |
| if (!query) { | |
| return res.status(400).json({message: 'query is missing'}); | |
| } | |
| var opts = { | |
| query: query, | |
| lang: lang, | |
| tld: tld, | |
| resultsPerPage: 25, | |
| nextPages: 1 | |
| }; | |
| googleSearch(opts, function (err, results) { | |
| if (err) { | |
| return next(err); | |
| } | |
| res.json(results); | |
| }); | |
| }); | |
| app.listen(3322, function () { | |
| console.log('search service started on 3322'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "yes", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "express": "^4.12.3", | |
| "google": "^0.6.0" | |
| } | |
| } |
Author
Thanks a lot. All good now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've modified https://gist.github.com/alexbeletsky/a3ce02a05790e24a3176#file-app-js-L25 for that.