Skip to content

Commit

Permalink
logic to fetch data from Brapi and ssend to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Prom authored and Chen Prom committed Dec 27, 2024
1 parent f851e51 commit fffd27d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
23 changes: 13 additions & 10 deletions tools/grin/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
52 changes: 33 additions & 19 deletions tools/grin/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);

0 comments on commit fffd27d

Please sign in to comment.