Skip to content

Commit

Permalink
axios added
Browse files Browse the repository at this point in the history
  • Loading branch information
Luxshan2000 committed Sep 28, 2023
1 parent beee22b commit bc4f76e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 17 deletions.
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT = 5000
MONGODB_CONNECTION_URL = "mongodb+srv://luxshan:[email protected]/items?retryWrites=true&w=majority"
MONGODB_CONNECTION_URL = "mongodb+srv://luxshan:[email protected]/aws-pipeline?retryWrites=true&w=majority"
13 changes: 6 additions & 7 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ 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
}))

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()
Expand Down
21 changes: 21 additions & 0 deletions backend/src/controllers/addData.js
Original file line number Diff line number Diff line change
@@ -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!")
}
}
13 changes: 13 additions & 0 deletions backend/src/controllers/getData.js
Original file line number Diff line number Diff line change
@@ -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)
}

}
11 changes: 11 additions & 0 deletions backend/src/modals/Item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require("mongoose")


const Item = mongoose.model("items",{
name:{
type:String,
required:true
}
});

module.exports = Item;
9 changes: 9 additions & 0 deletions backend/src/routes/addRoutes.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions backend/src/routes/getRoutes.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 16 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="App">
<h1>This is an Demo Project for CICD Pipeline</h1>
Expand All @@ -11,7 +26,7 @@ function App() {
<hr/>

<AddItems />
<SeeItems />
<SeeItems items={items} />
</div>
);
}
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/components/SeeItems.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import React, { useEffect, useState } from 'react'
import React from 'react'


const dummy = ["Apple", "Orange", "Car"]


function SeeItems() {
const [items, setItems] = useState([])

useEffect(()=>{
setItems(dummy)
})
function SeeItems({items}) {




if(!items){
return <h1>Loading.....</h1>
}
return (
<div>
{items.map((item)=>(
<h6>{item}</h6>
<h6 key={item._id}>{item.name}</h6>
))}
</div>
)
Expand Down

0 comments on commit bc4f76e

Please sign in to comment.