Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
JSON Endpoint Example 2
node v4.9.1
version:
1.0.0
endpoint
share
tweet
Send a request like ?url=http://google.com, get back the title of the page.
var endpoint = require("@runkit/runkit/json-endpoint/1.0.0") var request = require("request") var cheerio = require("cheerio") endpoint(module.exports, function(req, done) { request(req.query.url, function(error, response, body) { if (error) return done({error: "could not retrieve page"}) var page = cheerio.load(body) done({ title: page("title").text() || "no title" }) }) })
Instead of promises, we're using callbacks, and explicitly calling "done" with whatever value we want to return to the caller.
Loading…
1 comment
posted
4 years ago
by
ego
https://x-b8spceq4y1lh.runkit.sh/?url=http://google.com
sign in
to comment