tonic's notebooks

  • Express-Endpoint Demo - /tonic/express-endpoint-demo
    Last edited 7 years ago
    var tonicExpress = require("notebook")("tonic/express-endpoint/1.0.0") // Just provide the exports object to the tonicExpress helper var app = tonicExpress(module.exports) var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.get("/", (req, res) => res.send("Hey!")) app.get("/:name", (req, res) => { res.send(`Hello ${req.params.name}!`) }) app.post("/echo-form", (req, res) => { var formData = Object.keys(req.body).map( k => `${k}: ${req.body[k]}` ) res.type("text/plain") res.send(formData.join("\n")) })
  • JSON API with Endpoint - /tonic/json-endpoint
    Last edited 7 years ago
    function jsonAPI(anExport, aFunction) { var tonicExpress = require("notebook")("tonic/express-endpoint/1.0.0") var bodyParser = require('body-parser'); var app = tonicExpress(anExport) app.set('json spaces', 2); app.use(require('compression')()) app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); // add CORS headers, so the API is available anywhere app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Expose-Headers", "tonic-rate-limit-remaining"); var reqHeaders = req.get("Access-Control-Request-Headers") if (reqHeaders) res.header("Access-Control-Allow-Headers", reqHeaders); var reqMethods = req.get("Access-Control-Request-Methods") if (reqMethods) res.header("Access-Control-Allow-Methods", reqMethods); next() }) app.all('/', async function(request, response, next) { try { var requestData = { method: request.method, body: request.body, query: request.query } var responseData = await (aFunction.length === 1 ? aFunction(requestData) : new Promise(function(resolve) { aFunction(requestData, resolve) })) if (responseData) response.json(responseData) else response.status(201).end() } catch(anException) { response.status(500) response.json({ message: "unable to process request", error: anException.toString() }) } }) return app; } module.exports = jsonAPI;
  • Endpoint with Express - /tonic/express-endpoint
    Last edited 7 years ago
    var express = require("express"); function tonicExpress(anExport) { var mount = express(); var app = express(); // "mount" is our root app, and it mounts "app" at your notebook path // which is available in the TONIC_MOUNT_PATH environment variable mount.use(process.env.TONIC_MOUNT_PATH || "", app); if (anExport) { anExport.tonicEndpoint = mount; } // overwrite .listen since it is not needed app.listen = function(){} // return the express instance for use by the caller return app; } module.exports = tonicExpress
  • Endpoint Demo - /tonic/endpoint-demo-1
    Last edited 7 years ago
    The world's simplest hit counter, using a hosted database service called Orchestrate: https://orchestrate.io
  • Hello, World API - /tonic/hello-world-api
    Last edited 7 years ago
    exports.tonicEndpoint = function(req, res) { res.end("Hello, World!"); }
  • Forecast - /tonic/city-forecast
    Last edited 7 years ago
    var R = require("ramda"); var cities = ["New York", "San Francisco"]; var forecast = require("notebook")("tonic/forecast/2.0.0"); var promises = R.map(forecast.threeDay, cities); R.zipObj(cities, await Promise.all(promises));
  • Forecast - /tonic/forecast
    Last edited 7 years ago
    var request = require('request-promise'); var _ = require('lodash'); var url = require('url'); var apiKey = process.env.OPENWEATHERMAP_KEY; function temperatureURL(city) { return url.format({ protocol: "http", host: "api.openweathermap.org", pathname: "data/2.5/forecast", query: {units: "imperial", mode: "json", q: city, APPID: apiKey} }); } async function threeDay(city) { if (!apiKey) { throw `<div> <h2>Whoops!</h2> <p>You need an API key to use this service. Sign up at <a href="http://home.openweathermap.org/users/sign_up"> openweathermap.org</a>, and grab your API key:<br /><br /> <img src="http://bit.ly/1GgoJZm" width="500" /></p> <p>Now, add <code>OPENWEATHERMAP_KEY</code> to your <a href="https://tonicdev.com/settings/environment"> environment settings</a>:<br /><br /> <img src="http://bit.ly/1kmRPfQ" width="500" /></p> </div>` } var response = await request(temperatureURL(city)); return _.map(JSON.parse(response)['list'], function(entry) { return entry["main"]["temp"]; }); }; module.exports = { threeDay: threeDay };
  • Telephone? - /tonic/telephone
    Last edited 7 years ago
    You probably played the game of telephone as a kid, but it's kind of boring as an adult. But what if we get the computer to play for us instead? First, let's require some utilities from other notebooks, and define a few of our own.
  • brain.js - /tonic/brain-js
    Last edited 7 years ago
    This library is a pure javascript implementation of a neural network. Let's try running one of the examples in the README:
  • ES6 Binary and Octal Literals - /tonic/es6-binary-and-octal-literals
    Last edited 7 years ago
    JavaScript now allows you to write numbers using binary and octal: