JSON Endpoint Example 1

node v4.9.1
version: 1.0.0
Send a request to a URL like https://tonicdev.io/tonic/json-endpoint-example-1/branches/master?url=http://google.com, get back the title of the page in the url query parameter.
var endpoint = require("notebook")("tonic/json-endpoint/1.0.0") var requestPromise = require("request-promise") var cheerio = require("cheerio") endpoint(module.exports, async function(request) { try { var html = await requestPromise(request.query.url) var page = cheerio.load(html) } catch(e) { return {error: "could not retrieve page"} } return { title: page("title").text() || "no title" } })
Because we're using async/await, we're automatically returning a Promise. You can always explicitly return a promise too.

no comments

    sign in to comment