Created
February 7, 2023 09:10
-
-
Save Tribhuwan-Joshi/3f501c64ada3933ab6ec025876c2e993 to your computer and use it in GitHub Desktop.
Courses API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const mogoose = require('mongoose'); | |
const categories = require('./Routes/categories'); // import router with route name | |
mogoose.connect('mongodb://127.0.0.1/eapp').then(()=>console.log("Connection successful")).catch(err=>console.error("Couldn't connect to monogodb",err)); | |
app.use(express.json()); | |
app.use('/app/categories' ,categories); // routes | |
const port = process.env.PORT || 3000; | |
app.listen(port, () => { | |
console.log(`listening on port ${port}`); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// present inside /Routes | |
const express = require("express"); | |
const Joi = require("joi"); | |
const mongoose = require("mongoose"); | |
const app = express(); | |
app.use(express.json()) | |
const router = express.Router(); // use express.Router() , add all apis - get / put / delete to router . import router in app.js | |
// let categories = [ | |
// { id: 1, name: "Web" }, | |
// { id: 2, name: "Mobile" }, | |
// { id: 3, name: "Photography" }, | |
// ]; | |
const categorySchema = mongoose.Schema({ | |
name:{type:String , required:true} | |
}) | |
const Category = mongoose.model('Category' ,categorySchema ); | |
router.get("/", async (req, res) => { | |
let categories = await Category.find(); | |
res.send(categories); | |
}); | |
router.post("/", async (req, res) => { | |
const {error} = validateData(req.body); | |
if(error) res.status(400).send(error.details[0].message) | |
const category = new Category({name:req.body.name}) | |
await category.save(); | |
res.send(category); | |
}); | |
router.put("/:id", async (req, res) => { | |
const {error} = validateData(req.body); | |
if(error) res.status(400).send(error.details[0].message) | |
let category = await Category.findById(req.params.id) | |
if (!category) return res.status(404).send("The category is not found"); | |
else{ category.name = req.body.name; | |
await category.save(); | |
res.send(category) | |
} | |
}); | |
router.delete("/:id", async (req, res) => { | |
const course = await Category.findByIdAndDelete(req.params.id); | |
res.send(course); | |
}); | |
router.get("/:id", async (req, res) => { | |
const category = await Category.findById(req.params.id); | |
if (!category) return res.status(404).send("The category is not found"); | |
res.send(category); | |
}); | |
function validateData(category){ | |
const schema =Joi.object({ | |
name:Joi.string().min(3).required() | |
}) | |
return schema.validate(category) | |
} | |
module.exports =router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment