JSON Endpoint Example 2

node v5.12.0
version: 1.0.0
endpointsharetweet
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…

no comments

    sign in to comment