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("notebook")("tonic/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…
2 comments
posted
8 years ago
by
confidentattire123
thank you
posted
8 years ago
by
confidentattire123
?urlhttp://google.com
sign in
to comment