Created
May 17, 2020 12:30
-
-
Save RizkyRajitha/041bb41f46647a444abd3724a3cb4266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const cors = require("cors"); | |
const bp = require("body-parser"); | |
const fetch = require("node-fetch"); | |
app.use(cors()); | |
app.use(bp.urlencoded({ extended: false })); | |
app.use(bp.json()); | |
app.use(require("morgan")("dev")); | |
const AIRTABLEAPI = require("./config/env").airtableapikey; // import airtable api key | |
const AIRTABLEBASEID = require("./config/env").airtablebaseid; // import airtable base id | |
const AIRTABLETABLENAME = "seriescharacters"; // table name | |
const port = process.env.PORT || 5000; | |
app.get("/view", (req, res) => { | |
// to read a record , we need to send a "GET" request with our base id table name and our API key to get the existing data on our table. | |
fetch( | |
`https://api.airtable.com/v0/${AIRTABLEBASEID}/${AIRTABLETABLENAME}?maxRecords=3&view=Grid%20view`, | |
{ | |
headers: { Authorization: `Bearer ${AIRTABLEAPI}` }, | |
} | |
) | |
.then((res) => res.json()) | |
.then((result) => { | |
console.log(result); | |
//send results to the client | |
res.json(result); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
app.post("/create", (req, res) => { | |
console.log(req.body); | |
//read body from request | |
var datain = req.body; | |
//create payload in accepted format | |
var payload = { | |
records: [ | |
{ | |
fields: datain, | |
}, | |
], | |
}; | |
// to create a record we need to send a "POST" request with our base id, table name, our API key, and send a body with the new data we wish to add. | |
fetch(`https://api.airtable.com/v0/${AIRTABLEBASEID}/${AIRTABLETABLENAME}`, { | |
method: "post", | |
body: JSON.stringify(payload), | |
headers: { | |
Authorization: `Bearer ${AIRTABLEAPI}`, | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then((res) => res.json()) | |
.then((result) => { | |
console.log(result); | |
//send results to the client | |
res.json(result); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
app.post("/update", (req, res) => { | |
console.log(req.body); | |
//read body from request | |
var datain = req.body; | |
//create payload in accepted format | |
var payload = { | |
records: [ | |
{ | |
id: datain.id, | |
fields: datain.updatedata, | |
}, | |
], | |
}; | |
//to update a record we have to send the new record with it's the id to Airtable API. | |
fetch(`https://api.airtable.com/v0/${AIRTABLEBASEID}/${AIRTABLETABLENAME}`, { | |
method: "patch", | |
body: JSON.stringify(payload), | |
headers: { | |
Authorization: `Bearer ${AIRTABLEAPI}`, | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then((res) => res.json()) | |
.then((result) => { | |
console.log(result); | |
res.json(result); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
app.post("/delete", (req, res) => { | |
console.log(req.body); | |
//we need to send a "DELETE" request with our base id table name, the id of the record we wish to delete and our API key to get the existing data on our table. | |
fetch( | |
`https://api.airtable.com/v0/${AIRTABLEBASEID}/${AIRTABLETABLENAME}/${req.body.id}`, | |
{ | |
method: "delete", | |
// body: JSON.stringify(payload), | |
headers: { | |
Authorization: `Bearer ${AIRTABLEAPI}`, | |
// "Content-Type": "application/json", | |
}, | |
} | |
) | |
.then((res) => res.json()) | |
.then((result) => { | |
console.log(result); | |
res.json(result); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
app.listen(port, () => { | |
console.log("listning on " + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment