diff --git a/backend/.env b/backend/.env index ff15c18..2201e53 100644 --- a/backend/.env +++ b/backend/.env @@ -1,2 +1,2 @@ PORT = 5000 -MONGODB_CONNECTION_URL = "mongodb+srv://luxshan:luxshan123@cluster0.ztjzj8h.mongodb.net/items?retryWrites=true&w=majority" \ No newline at end of file +MONGODB_CONNECTION_URL = "mongodb+srv://luxshan:luxshan123@cluster0.ztjzj8h.mongodb.net/aws-pipeline?retryWrites=true&w=majority" \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index eb00309..47484f8 100644 --- a/backend/server.js +++ b/backend/server.js @@ -2,17 +2,16 @@ const express = require("express") const cors = require("cors") const bodyParser = require("body-parser") - const connectMongoDb = require("./config/database") -// const addRoutes = require('./src/routes/addRoutes'); -// const getRoutes = require('./src/routes/getRoutes'); +const getRoutes = require('./src/routes/getRoutes') +const addRoutes = require('./src/routes/addRoutes') const app = express() const PORT = process.env.PORT || 5000 app.use(cors({ - origin:["*"],//"exp://192.168.8.182:8081" - methods:["GET"], + origin:["*", "http://51.20.93.112:3000"],//"exp://192.168.8.182:8081" + methods:["GET","POST"], credentials:true })) @@ -20,8 +19,8 @@ app.use(bodyParser.json()) // app.use('/api/add', addRoutes); -// app.use('/api/get', getRoutes); - +app.use('/api/get', getRoutes); +app.use('/api/add', addRoutes); connectMongoDb() diff --git a/backend/src/controllers/addData.js b/backend/src/controllers/addData.js new file mode 100644 index 0000000..1e2307e --- /dev/null +++ b/backend/src/controllers/addData.js @@ -0,0 +1,21 @@ +const Item = require('../modals/Item') + + + +exports.addItem = async (req,res) =>{ + try{ + + const { name } = req.body + + const newItem = new Item({ name}) + await newItem.save() + + + console.log("Successfully meal added!") + + + + } catch(error){ + console.log("There is an error occured in addMeal!") + } +} \ No newline at end of file diff --git a/backend/src/controllers/getData.js b/backend/src/controllers/getData.js new file mode 100644 index 0000000..9336f37 --- /dev/null +++ b/backend/src/controllers/getData.js @@ -0,0 +1,13 @@ +const Item = require('../modals/Item') + + +exports.getAllItem = async (req,res)=>{ + + try{ + const items = await Item.find({}); + res.json(items) + }catch(err){ + res.json(err) + } + +} \ No newline at end of file diff --git a/backend/src/modals/Item.js b/backend/src/modals/Item.js new file mode 100644 index 0000000..bfb8a4b --- /dev/null +++ b/backend/src/modals/Item.js @@ -0,0 +1,11 @@ +const mongoose = require("mongoose") + + +const Item = mongoose.model("items",{ + name:{ + type:String, + required:true + } +}); + +module.exports = Item; \ No newline at end of file diff --git a/backend/src/routes/addRoutes.js b/backend/src/routes/addRoutes.js new file mode 100644 index 0000000..c1a395e --- /dev/null +++ b/backend/src/routes/addRoutes.js @@ -0,0 +1,9 @@ +const express = require('express'); +const router = express.Router(); +const addData = require('../controllers/addData'); + + +router.get('/items', addData.addItem) + + +module.exports = router; \ No newline at end of file diff --git a/backend/src/routes/getRoutes.js b/backend/src/routes/getRoutes.js new file mode 100644 index 0000000..5dd4f33 --- /dev/null +++ b/backend/src/routes/getRoutes.js @@ -0,0 +1,9 @@ +const express = require('express'); +const router = express.Router(); +const getData = require('../controllers/getData'); + + +router.get('/items', getData.getAllItem) + + +module.exports = router; \ No newline at end of file diff --git a/frontend/src/App.js b/frontend/src/App.js index c6a7e11..40a3d30 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -1,8 +1,23 @@ import './App.css'; import AddItems from './components/AddItems'; import SeeItems from './components/SeeItems'; +import axios from 'axios'; +import { useEffect, useState } from 'react'; function App() { + const [items, setItems] = useState([]) + + useEffect(()=>{ + axios.get("http://51.20.93.112:5000/api/get/items").then(response => { + + setItems(response.data) + }) + .catch(error => { + // Handle any errors here + console.log('Error:'); + }); + },[]) + return (