Question: What code changes do I need to make here to handle the DataForm? Thank you. const express = require ( express ) ;

What code changes do I need to make here to handle the DataForm? Thank you.
const express = require("express");
const cors = require("cors");
const multer = require("multer");
const mongoose = require("mongoose");
const Joi = require("joi");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
const mongoURI = process.env.MONGODB_URI || "mongodb://localhost:27017/gearInventory";
mongoose.connect(mongoURI,{ useNewUrlParser: true, useUnifiedTopology: true })
.then(()=> console.log("MongoDB connected successfully"))
.catch((err)=> console.error("MongoDB connection error:", err));
const gearSchema = new mongoose.Schema({
name: { type: String, required: true },
brand: { type: String, required: true },
price: { type: Number, required: true },
img_name: { type: String },
rating: { type: Number },
features: [String],
});
const Gear = mongoose.model("Gear", gearSchema);
const storage = multer.diskStorage({
destination: (req, file, cb)=>{
cb(null,"./public/images/");
},
filename: (req, file, cb)=>{
cb(null, Date.now()+"-"+ file.originalname);
},
});
const upload = multer({ storage });
const itemSchema = Joi.object({
name: Joi.string().required(),
brand: Joi.string().required(),
price: Joi.number().required(),
img_name: Joi.string().optional(),
rating: Joi.number().optional(),
features: Joi.array().items(Joi.string()).optional(),
});
app.get("/api/gear", async (req, res)=>{
try {
const items = await Gear.find();
res.json(items);
} catch (err){
res.status(500).json({ success: false, message: "Failed to fetch gear items" });
}
});
app.post("/api/gear", async (req, res)=>{
const { error }= itemSchema.validate(req.body);
if (error){
return res.status(400).json({ success: false, message: error.details[0].message });
}
try {
const newItem = new Gear(req.body);
await newItem.save();
res.status(201).json({ success: true, newItem });
} catch (err){
res.status(500).json({ success: false, message: "Failed to add gear item" });
}
});
app.put("/api/gear/:id", async (req, res)=>{
const { error }= itemSchema.validate(req.body);
if (error){
return res.status(400).json({ success: false, message: error.details[0].message });
}
try {
const updatedItem = await Gear.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!updatedItem){
return res.status(404).json({ success: false, message: "Item not found" });
}
res.json({ success: true, updatedItem });
} catch (err){
res.status(500).json({ success: false, message: "Failed to update gear item" });
}
});
app.delete("/api/gear/:id", async (req, res)=>{
try {
const deletedItem = await Gear.findByIdAndDelete(req.params.id);
if (!deletedItem){
return res.status(404).json({ success: false, message: "Item not found" });
}
res.json({ success: true, message: "Item deleted successfully" });
} catch (err){
res.status(500).json({ success: false, message: "Failed to delete gear item" });
}
});
const PORT = process.env.PORT ||4000;
app.listen(PORT,()=> console.log(`Listening on port ${PORT}...`));

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!