Skip to content

Instantly share code, notes, and snippets.

@mgonto
Last active November 19, 2015 21:57
Show Gist options
  • Save mgonto/33377a90a318c2214f30 to your computer and use it in GitHub Desktop.
Save mgonto/33377a90a318c2214f30 to your computer and use it in GitHub Desktop.
Minimal WebTask Runner
node_modules/

Webtask Runner

This is minimal web server made in Node which runs WebTasks.

Installing it

Clone the repository and run

npm i
node start.js

Simple usage example

This server has no UI, but you can use its API.

For example, with the following code you can run the hello world in the server which will use the query parameter who to state the person's name.

curl 'http://127.0.0.1:3001/run?who=my%20dear%20friend' -H 'Content-Type: text/plain;charset=UTF-8' --data-binary 'return function(ctx, cb) { cb(null, {message: "Hello " + ctx.who}); }'
{
"name": "minimal-webtask-runner",
"version": "0.1.0",
"description": "A runner for WebTasks",
"main": "start.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"webtask",
"runner",
"eval",
"webtask.io"
],
"author": "Martin Gontovnikas <[email protected]> (http://gon.to/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/auth0/webtask-runner/issues"
},
"homepage": "https://github.com/auth0/webtask-runner",
"dependencies": {
"babel": "^5.1.4",
"babel-core": "^5.1.4",
"body-parser": "^1.12.2",
"compression": "^1.4.3",
"cors": "^2.5.3",
"dotenv": "^1.1.0",
"errorhandler": "^1.3.5",
"express": "^4.12.3",
"express-jwt": "^3.0.0",
"jsonwebtoken": "^5.0.0",
"lodash": "^3.6.0",
"morgan": "^1.5.2",
"request": "^2.55.0"
}
}
import logger from 'morgan';
import cors from 'cors';
import http from 'http';
import express from 'express';
import errorhandler from 'errorhandler';
import bodyParser from 'body-parser';
let app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(cors());
app.use(logger('dev'));
app.use(errorhandler())
// Main page
app.get('/', (req, res) => {
res.send("Server started").status(200);
});
// Task runner
app.post('/run', (req, res) => {
let clientCode = null;
try {
let factory = new Function(req.body);
clientCode = factory();
if (typeof clientCode !== 'function') {
let msg = 'The code does not return a JavaScript function.';
res.status(400).send(msg);
}
if (clientCode.length === 0 || clientCode.length > 2) {
let msg = 'The JavaScript function must have one of the following signature: (ctx, callback)';
res.status(400).send(msg);
}
} catch (e) {
let msg = 'Unable to compile submitted JavaScript. ' + e.toString();
res.status(500).send(msg);
}
let args = [];
if (clientCode.length === 2) {
args.push(req.query);
}
args.push((err, data) => {
if (err) {
let msg = 'Script returned error.';
res.status(400).send(msg);
}
let returnBody = null;
try {
returnBody = data ? JSON.stringify(data): '{}';
} catch (e) {
let msg = 'Error when JSON serializing the result of the JavaScript code.';
res.status(400).send(msg);
}
res.set('Content-Type', 'application/json').status(200).send(returnBody);
});
try {
clientCode.apply(this, args);
} catch(e) {
let msg = 'Script generated an unhandled synchronous exception.';
res.status(500).send(msg);
}
});
let port = process.env.PORT || 3001;
http.createServer(app).listen(port, function (err) {
console.log('listening in http://localhost:' + port);
});
export default app;
// Use ES6 with Babel until Node has all features
require("babel/register");
require('./server');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment