Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

JSON API with Endpoint

node v4.9.1
version: 1.0.0
endpointsharetweet
function jsonAPI(anExport, aFunction) { var runkitExpress = require("@runkit/runkit/express-endpoint/1.0.0") var bodyParser = require('body-parser'); var app = runkitExpress(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", "runkit-rate-limit-remaining"); 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 === 2 ? (new Promise(function(resolve) { aFunction(requestData, resolve) })) : aFunction(requestData)) 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;
This helper creates a simple JSON API that accepts JSON request data and returns a JSON response. It can be used two different ways, depending on the callback's signature. The incoming data is an object with three properties: - "method": the request method used - "body": the data sent in the body of the request - "query": any query parameters sent in the request If the "arity" (the number of arguments) of the provided callback function is 1, then you can return a response directly, or a promise that eventually resolves to the data. Here's an example use: https://runkit.com/runkit/json-endpoint-example-1 If the "arity" is 2, then the second parameter is a "done" callback that you should call yourself whenever you want to return a response. You can pass the response as the only argument to the callback. Here's an example use: https://runkit.com/runkit/json-endpoint-example-2
Loading…

6 comments

  • posted 4 years ago by methradeth
    what are the arguments "anExport" and "aFunction" and where are the passed in?
  • posted 4 years ago by alphanumeric010101
    I think those are just placeholder for "aFunction" that you would write yourself. anException is, likewise, an error that you are watching for.
  • posted 2 years ago by alfian547
    https://runkit.com/assets/images/spaceman.jpg
  • posted a year ago by iloveitaly
    It looks like this package only works up to node v6.17.1. Is that correct? Is there another library we should be using?
  • posted 6 months ago by 5e9cc003debef1001a5391de
    ECMAScript 6 (also known as ES2015) Support:
  • posted 6 months ago by 5e9cc003debef1001a5391de
    Runkit Save Account JSON API with Endpoint node v4.9.1version: 1.0.0endpointsharetweet 1 function jsonAPI(anExport, aFunction) 2 { 3 var runkitExpress = require("@runkit/runkit/express-endpoint/1.0.0") 4 var bodyParser = require('body-parser'); 5 var app = runkitExpress(anExport) 6 ​ 7 app.set('json spaces', 2); 8 app.use(require('compression')()) 9 app.use(bodyParser.json()); 10 app.use(bodyParser.urlencoded({ extended: false })); 11 ​ 12 // add CORS headers, so the API is available anywhere 13 app.use(function(req, res, next) 14 { 15 res.header("Access-Control-Allow-Origin", "*"); 16 res.header("Access-Control-Expose-Headers", "runkit-rate-limit-remaining"); 17 res.header("Access-Control-Expose-Headers", "tonic-rate-limit-remaining"); 18 19 var reqHeaders = req.get("Access-Control-Request-Headers") 20 if (reqHeaders) res.header("Access-Control-Allow-Headers", reqHeaders); 21 22 var reqMethods = req.get("Access-Control-Request-Methods") 23 if (reqMethods) res.header("Access-Control-Allow-Methods", reqMethods); 24 ​ 25 next() 26 }) 27 ​ 28 app.all('/', async function(request, response, next) 29 { 30 try 31 { 32 var requestData = { 33 method: request.method, 34 body: request.body, 35 query: request.query 36 } 37 ​ 38 var responseData = await (aFunction.length === 2 ? 39 (new Promise(function(resolve) { 40 aFunction(requestData, resolve) 41 })) : aFunction(requestData)) 42 ​ 43 if (responseData) 44 response.json(responseData) 45 else 46 response.status(201).end() 47 } 48 catch(anException) 49 { 50 response.status(500) 51 response.json({ 52 message: "unable to process request", 53 error: anException.toString() 54 }) 55 } 56 }) 57 58 return app; 59 } 60 ​ 61 module.exports = jsonAPI; function jsonAPI() This helper creates a simple JSON API that accepts JSON request data and returns a JSON response. It can be used two different ways, depending on the callback's signature. The incoming data is an object with three properties: ​ - "method": the request method used - "body": the data sent in the body of the request - "query": any query parameters sent in the request ​ If the "arity" (the number of arguments) of the provided callback function is 1, then you can return a response directly, or a promise that eventually resolves to the data. Here's an example use: https://runkit.com/runkit/json-endpoint-example-1 ​ If the "arity" is 2, then the second parameter is a "done" callback that you should call yourself whenever you want to return a response. You can pass the response as the only argument to the callback. Here's an example use: https://runkit.com/runkit/json-endpoint-example-2 5 comments posted 3 years ago by methradeth what are the arguments "anExport" and "aFunction" and where are the passed in? posted 3 years ago by alphanumeric010101 I think those are just placeholder for "aFunction" that you would write yourself. anException is, likewise, an error that you are watching for. posted a year ago by alfian547 https://runkit.com/assets/images/spaceman.jpg posted 9 months ago by iloveitaly It looks like this package only works up to node v6.17.1. Is that correct? Is there another library we should be using? posted a few seconds ago by 5e9cc003debef1001a5391de delete ECMAScript 6 (also known as ES2015) Support: Add a comment

sign in to comment