Skip to content

Instantly share code, notes, and snippets.

@ahandsel
Created October 2, 2022 04:07
Show Gist options
  • Save ahandsel/31ee9b778e01e1ad262e692b5ffef2fb to your computer and use it in GitHub Desktop.
Save ahandsel/31ee9b778e01e1ad262e692b5ffef2fb to your computer and use it in GitHub Desktop.
// backend - server.js - Routes API requests from the frontend to Kintone
// Express Server Setup
const express = require('express');
const cors = require('cors');
const fetch = require('node-fetch');
const PORT = 5000;
const app = express();
// Hide sensitive info in a .env file with dotenv
require('dotenv').config();
// Get Kintone credentials from a .env file
const subdomain = process.env.SUBDOMAIN;
const appID = process.env.APPID;
const apiToken = process.env.APITOKEN;
// Parse incoming requests with JSON payloads
app.use(express.json());
// Set Cross-Origin Resource Sharing (CORS) to frontend React App
app.use(cors());
const corsOptions = {
origin: 'http://localhost:3000'
};
// Append a query parameter to the request endpoint
// This query orders records by their Record_number in ascending order
const parameters = 'query=order by Record_number asc';
// Kintone's record(s) endpoints
const multipleRecordsEndpoint = `https://${subdomain}.kintone.com/k/v1/records.json?app=${appID}&${parameters}`
const singleRecordEndpoint = `https://${subdomain}.kintone.com/k/v1/record.json?app=${appID}`;
// This route executes when a GET request lands on localhost:5000/getData
app.get('/getData', cors(corsOptions), async (req, res) => {
const fetchOptions = {
method: 'GET',
headers: {
'X-Cybozu-API-Token': apiToken
}
}
const response = await fetch(multipleRecordsEndpoint, fetchOptions);
const jsonResponse = await response.json();
res.json(jsonResponse);
});
/* Add a new route for a POST request using singleRecordEndpoint in the section below */
// - - - - - - - START - - - - - - - -
// This runs if a POST request calls for localhost:5000/postData
app.post('/postData', cors(corsOptions), async (req, res) => {
const requestBody = {
'app': appID,
'record': {
'title': {
'value': req.body.title
},
'author': {
'value': req.body.author
}
}
};
const options = {
method: 'POST',
headers: {
'X-Cybozu-API-Token': apiToken,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
}
const response = await fetch(singleRecordEndpoint, options);
const jsonResponse = await response.json();
res.json(jsonResponse);
});
// - - - - - - - END - - - - - - - -
app.listen(PORT, () => {
console.log(`\n Backend server listening at http://localhost:${PORT} \n Confirm if Kintone records are being retrieved at \n http://localhost:${PORT}/getData`);
});
@ahandsel
Copy link
Author

ahandsel commented Oct 2, 2022

If you want to change what data you are pulling from Kintone, you need to update the server.js with the field code of the fields you want.

This code is pulling data from fields with field codes title and author specified in the following lines:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment