air_table_proxy

node v10.24.1
version: 3.0.0
endpointsharetweet
// set up the endpoint. exports.endpoint = function(request, response) { // start up the node.js web client var https = require('https'); // get the search string "aq" from the querystring var aq = require('url').parse(request.url, true).query.aq; // process.env.app_id is something we set up in the Runkit environment variables. // this keeps us from having to expose the Airtable secret key! var app_id = process.env.app_id; var app_key = process.env.app_key; // set the URL for Airtable var url = "api.airtable.com"; // set the "path" for the options below. Node.js https client can use url/path rather than combining them. var mypath = "/v0/" + app_id + '/Albums?view=Grid view&filterByFormula=FIND(LOWER("' + aq + '"),LOWER({Album Name}))'; // the path needs to be encoded for Airtable to read it var encodedmypath = encodeURI(mypath); // these are for the node.js https client var options = { host: url, port: 443, path: encodedmypath, method: 'GET', headers: { 'Authorization': 'Bearer ' + app_key, 'Accept': 'application/json' } }; // we need to define the Airtable result variable outside of the https request below or we won't be // able to access the result outside of the .on('data'.. call var atResult = ''; // open the https connection to Airtable using the options set above https.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { // data is now coming in from Airtable // node.js will split a large request into "chunks" and deliver multiple chunks. // we have to combine the chunks using += and append to our atResult variable. atResult += chunk; }); res.on('end', function () { // the call to Airtable is complete. // now we have to send the results to the caller of this code. response.writeHead(200, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': '*', }); // send the result! response.end(atResult); }); }).end(); // end the https client }
Loading…

no comments

    sign in to comment