diff --git a/tools/grin/scripts.js b/tools/grin/scripts.js index f642e585..44990711 100644 --- a/tools/grin/scripts.js +++ b/tools/grin/scripts.js @@ -218,16 +218,19 @@ function fetchDataPost(){ } async function getBrapi(){ - const url = "https://npgsweb.ars-grin.gov/gringlobal/brapi/v2/traits?commonCropName=GLYCINE-PERENNIAL"; - try { - const response = await fetch(url); + fetch('http://localhost:3000/brapi') // Replace with your Express server URL + .then(response => { if (!response.ok) { - throw new Error(`Response status: ${response.status}`); + throw new Error('Network response was not ok'); } - - const json = await response.json(); - console.log(json); - } catch (error) { - console.error(error.message); - } + return response.json(); // Assuming your server sends JSON data + }) + .then(data => { + // Do something with the fetched data + console.log(data); + }) + .catch(error => { + // Handle errors + console.error('There has been a problem with your fetch operation:', error); + }); } \ No newline at end of file diff --git a/tools/grin/server/server.js b/tools/grin/server/server.js index 00fc43a7..e8c8f7fb 100644 --- a/tools/grin/server/server.js +++ b/tools/grin/server/server.js @@ -27,28 +27,42 @@ MongoClient.connect(MONGODB_URI) } }); - app.post('/data1', async (req, res) => { - console.log(req.body.id) - }); - - app.get('/brapi', async (req, res) => { - let requestOptions = { - method: 'GET', - redirect: 'follow' - }; - - fetch("https://npgsweb.ars-grin.gov/gringlobal/brapi/v2/traits?commonCropName=GLYCINE-PERENNIAL", requestOptions) - .then(response => response.text()) - .then(result => console.log(result)) - .catch(error => console.log('error', error)); - }) - - app.listen(PORT, () => { - console.log(`Server listening on port ${PORT}`); - }); }) .catch(err => { console.error('Error connecting to MongoDB:', err); }); + + + + + + +// const express = require('express'); +// const {MongoClient} = require('mongodb'); +// const cors = require('cors') + +// const app = express(); +// const PORT = process.env.PORT || 3000; +// const MONGODB_URI = 'mongodb://localhost:27017/'; + + +// app.use(express.json()); +// app.use(cors()); + +app.get('/brapi', (req, res)=>{ + res.status(200); + fetch("https://npgsweb.ars-grin.gov/gringlobal/brapi/v2/traits?commonCropName=GLYCINE-PERENNIAL") + .then(response => response.text()) + .then(result => res.send(result)) + .catch(error => console.log('error', error)); +}); + +app.listen(PORT, (error) =>{ + if(!error) + console.log("Server is Successfully Running, and App is listening on port "+ PORT) + else + console.log("Error occurred, server can't start", error); + } +); \ No newline at end of file