Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

connecting koa js backend with parcel front

Item Model.js

const mongoose = require("mongoose")
const ItemSchema = new mongoose.Schema({
    itemId: {type:String, required: true},
    itemName : {type: String, required: true},
})
const Item = mongoose.model("items", ItemSchema);
module.exports = Item;

ItemController.js file here you have the apiis

const Item = require("../Models/ItemModel");

add item function in here used itemId and Item Name

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const addItem = async(ctx)=>{    
    try {
        const {itemId,itemName} = ctx.request.body;
        const item =await Item.create({
            itemName,
            itemId,
        })
        return (ctx.body = item);
    } catch (error) {
        return (ctx.body ={message : error.message})
    }
}

get item function this means get all items

const getItems = async(ctx)=>{
    try {
        const item = await Item.find();
        return(ctx.body = item)
    } catch (error) {
        return (ctx.body ={message : error.message})
    }
}

delete items function inhere we need to pass the id to identify the specifit item amoung all of them

const deleteItems = async(ctx)=>{
    try {
        const itemid = ctx.params.id;
        const item = await Item.findByIdAndDelete(itemid);
        return (ctx.body = item);
    } catch (error) {
        return (ctx.body ={message : error.message})
    }
}

find by id method in here we get id and find the item bu

const findByIDItems = async(ctx)=>{
    try {
        const itemid = ctx.params.id;
        const item = await Item.findById(itemid)
        return (ctx.body = item);
    } catch (error) {
        return (ctx.body ={message : error.message})
    }
}

update item method in this method we used mongoose to update the query

const updateItem = async(ctx)=>{
    try {
        const {itemName,itemId} = ctx.request.body;
        const itemid = ctx.params.id;
        const item = await Item.findByIdAndUpdate(itemid,{
            itemName,
            itemId,
        })
        return (ctx.body = item)
    } catch (error) {
        return (ctx.body ={message : error.message})
        
    }
}

exporting

module.exports = {addItem,getItems,deleteItems,findByIDItems,updateItem}
   

Router file

  const KoaRouter = require('@koa/router');
  const router = new KoaRouter({prefix:"/items"});
  const {addItem,getItems,deleteItems,findByIDItems,
  updateItem}=require("../Controller/ItemController")

routers and links

router.post("/add",addItem);
router.get("/",getItems);
router.put("/:id",updateItem);
router.get("/:id",findByIDItems);
router.delete("/:id",deleteItems);

exporting the module

module.exports=router;

this is DB connection file you connect DB by this file

const mongoose  = require("mongoose");
const dbConnect = () =>{
    mongoose.connect('mongodb://localhost:27017',()=>{
               console.log("Db connected success")
    })
 }
 module.exports ={dbConnect}

>Solution :

this is server JS file for you

imported methods

const Koa = require('koa')
const KoaRouter = require('@koa/router');
const cors = require('@koa/cors')
const bodyParser = require('koa-bodyparser')
const json = require('koa-json')
const {dbConnect} = require('./Utils/DBconnect');
const itemRouter = require("./Routes/itemRouter");
const app = new Koa();
const router = new KoaRouter();

these are the middlewares

app.use(cors());
app.use(bodyParser());
app.use(json());
app.use(router.routes()).use(router.allowedMethods());
app.use(itemRouter.routes());

in here you listen to the app

app.listen(8000,()=>{
    dbConnect();
    console.log("server running")
    console.log("in 8000")
}) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading