const fs = require("fs");
// Make a new folder.
fs.mkdirSync("my-new-folder");
// Verify that the folder is on the filesystem.
fs.readdirSync("./").filter(aFile => aFile === "my-new-folder");
const express = require("express");
const app = express();
const port = 3000;
// use body-parser to automatically parse JSON-encoded request bodies
const bodyParser = require("body-parser");
app.use(bodyParser.json());
const helperMessage = { message: "Try posting JSON to this endpoint!" };
// add a helper message encouraging POSTing to this API
app.get("/", (req, res) => res.json(helperMessage));
// listen for POST requests and respond based on that POSTed data
app.post("/", (req, res) => {
if (req.body.key)
return res.send({ key: req.body.key });
return res.send({ message: `You posted ${JSON.stringify(req.body)}` });
});
app.listen(port, () => console.log(`Example API listening on port ${port}!`));
/*
Try it out by POSTing to this endpoint!
Get this API's URL from the Endpoint button at the top of the Notebook
curl -X "POST" <ENDPOINT URL> \
-H 'Content-Type: application/json; charset=utf-8' \
-d #x27;{ "key": "value" }'
*/
This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
var child_process = require("child_process");
var request = require("acorn");
var path = require.resolve("acorn");
exports.endpoint = (req, res) => {
var out = child_process.execSync(`node -e 'var x = new Date(); require("${path}"); console.log(new Date() - x);'`);
res.end(out)
}
var child_process = require("child_process");
var request = require("request");
var path = require.resolve("request");
exports.endpoint = (req, res) => {
var out = child_process.execSync(`node -e 'var x = new Date(); require("${path}"); console.log(new Date() - x);'`);
res.end(out)
}
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.