Quickstart: Add your first Node.js function to Blockspring.

In this tutorial we'll show you how to add a Node.js function to Blockspring. Doing so is helpful if you need to:

  • Expose your function to the world. Need some Node.js in your Python app, Android app, Arduino, or even in Google Spreadsheets? You'll be able to run your function from any application and device by simply copy/pasting a single line of code.

  • Share your function with anyone. You'll automatically generate a simple form and webpage documentation. Never spend time explaining your code, or running it for other people.

  • Combine Blockspring functions together. You can mix/match Blockspring functions like building blocks. And the system gets really fast when it knows ahead of time which functions you're about to run together.

  • Add really, really powerful caching. Don't just cache when you run a function, use Blockspring's social caching. What's the point of recalculating when someone's already done the calculating?

Let's get started!

Expose your function to the world.

This is going to be insanely fast. In 30 seconds, you're going to add a function to Blockspring that identifies the color palette of apple.com.

var webshot = require('webshot');
var thief = require('color-thief');

var colorThief = new thief;
webshot('apple.com', 'apple.png', function(err) {
    var palette = colorThief.getPalette("apple.png", 8);

Click to populate the Blockspring editor with the code above, click Publish, and you're done!

What just happened?

When you published your function, Blockspring automatically deployed it to the cloud, gave it an API, generated a webpage, and exposed it to all of the Blockspring client libraries.

Why is this awesome?

Your Node.js function is now in the cloud. That means you can run it from any application or internet-enabled device with a single command. What once took 6 lines of Node.js, now is 1 line in any language. For instance, to run your color palette grabber in R, just type the following:


# call palette grabber (https://open.blockspring.com/blocks/98d39b743fdd0dd361c3081b5323a4cf)

Where do I learn more about using my function?

Your published function should like something like this palette grabber. On each Blockspring function page you can click "Integrations", and depending on where you need to use your function, copy/paste the associated one line of code to run it. Check out the run Node.js in Blockspring quickstart to learn more.

That was fast. Can you show me how to publish again?

Here's a quick video that shows you how to publish step-by-step.

Share your function with anyone.

Blockspring lets you share your work with programmers and non-programmers alike because we generate the webpage and docs.

Currently, the webpage for your palette grabber function is just a "Submit" button. That's because this function doesn't take any inputs.

Submit only form

Let's tell Blockspring that your function accepts inputs. For instance, try grabbing the pallete from any website, not just Apple.

Click to populate the Blockspring editor with the code below, click Publish, and you'll have a nice form for your function.

var webshot = require('webshot');
var thief = require('color-thief');
var blockspring = require('blockspring');

blockspring.define(function(request, response){
    var colorThief = new thief;
    webshot(request.params["url"], 'image.png', function(err) {
        var palette = colorThief.getPalette("image.png", 8);
        response.addOutput("colors", palette);

Take a look at the final product. Here's a video that shows the process step-by-step:

What just happened?

You updated your function in four ways:

  • Import the library. You imported the blockspring library with require("blockspring"). Blockspring captures outputs by reading what is printed. That's why we're suppressing loading messages.
  • Define the function. You wrapped the function in a blockspring.define(). This tells Blockspring what function run.
  • Get the inputs. Receiving the url input in request.params["url"]. All function inputs will be found in request.params. The "url" name is defined when you added a parameter through the Blockspring code editor.
  • Declare the outputs. Adding the color pallette output using response.addOutput. Blockspring passes inputs and outputs as JSON. This is adding your output to the JSON response.
Learn more with the Node.js API reference.

Why is this awesome?

You can now share a parameterized function without doing any extra work. By telling Blockspring about your inputs, you automatically get a form so anyone can run your function manually. Learn more about input parameter types.

Wait - how do I run my function in other languages with inputs?

Check out running Node.js functions to get a refresher on running Blockspring functions. Here's the updated R one-liner:


# call palette grabber (https://open.blockspring.com/blocks/91be8b9acdf7e32662373ee244912418)
blockspringRunParsed("91be8b9acdf7e32662373ee244912418", list(
  url = "http://www.twitter.com"

While we're at it, we might want to call our palette grabber from front-end JS too. Check out the example below (and this gist for the code). Change the website below, click enter, and you should see a new color palette.

We're running a back-end Node.js process right from Javascript without setting up any servers!

Now it's your turn.

Congratulations young grasshopper. You've learned to add a Node.js function to your Blockspring library. Write a function, get an application. Now go build!

Create a new function Work at Blockspring