WHAT IS API Application Program Interface NodeJs websites Android Application __API___ MongoDB,mysql Swift iphone app API ---Data json/XML/GRAPHQL MOSTLY USE JSON IN API EXPRESSJS NodeJs API DEVELOPMENT READ app.get('/profile',(req,res)=>{}) Create app.post('/profile',(req,res)=>{}) Update app.put('/profile',(req,res)=>{}) Delete app.delete('/profile',(req,res)=>{}) notes But work with HTML use only post ,get But Show Your data Use method res.json() middleware app.use(express.json()) need command npm init -y npm install express nodemon mongoose create index.js create.models,routes folder files add in index.js const studentRouterfiles=require('./routes/students.routes') add in routes folder module.exports=router const StudentsModel=require('../models/students.model') open command mongosh show dbs ,use students-crud,show collections db.mystudentcolls.insertOne({ first_name:"Rupesh", last_name:'tech', email:'admin@admin.com', phone:"67465456564", gender:"male" }) db.mystudentcolls.insertOne({ first_name:"sajan", last_name:'kumar', email:'sajan@admin.com', phone:"9965456564", gender:"male" }) open POSTMEN M-Get localhost:3000/api/students // see data all M-Get localhost:3000/api/students/683a961a0a98305151b71236//single data M-POSt localhost:3000/api/students/ select params-body-form-urlencoded enter key and value M-PUT localhost:3000/api/students/683ac39fcb3f1618b4f05571 select params-body-form-urlencoded enter key and value app.js const express = require('express'); const app = express(); const mongoose=require('mongoose') const studentRouterfiles=require('./routes/students.routes') // router files import here and this route declare midleware also mongoose.connect('mongodb://127.0.0.1:27017/students-crud') //ALSO CAL SEPARTE IN .ENV FILES .then(()=>console.log("connected mongo")) .catch(err=>console.log(err)) // parse application/x-www-form-urlencoded app.use(express.urlencoded({ extended: false })) // middleware app.use(express.json()) // route midllware name =>app.use(routename/topic_api_name,Yourdefine_routefilesname) app.use('/api/students', studentRouterfiles) app.listen(3000, () => { console.log('Server running on port 3000'); }); model.js // create define schema const mongoose=require('mongoose') const studentSchema=new mongoose.Schema({ first_name: { type : String, require: true }, last_name: { type : String, require: true }, email: { type : String, require: true, unique: true }, phone: { type : String, require: true }, gender: { type : String, enum: ['Male','Female','Other'], require: true }, profile_pic: { type : String } }) // create collection name .YourSchema const Student =mongoose.model('mystudentColl',studentSchema) //Studnts var export module.exports=Student route.js const express=require('express') // use Router method for myroutes const router=express.Router() // add Your model folder const StudentsModel=require('../models/students.model') // const { model } = require('mongoose') /* Now create Your Route using Router method using error handing try{ }catch(err){ } */ // 1. See All data router.get('/',async(req,res)=>{ try{ const students_fetchall=await StudentsModel.find() res.json(students_fetchall) }catch(err){ res.status(500).json({message:err.message}) } }) // 3. See single data router.get('/:id',async(req,res)=>{ try{ const students_singledata=await StudentsModel.findById(req.params.id) // id se value lene ke lie req,params.id use krte h //before check data come or not may be id wrong if(!students_singledata) return res.status(404).json({message:'students not found'}) res.json(students_singledata) }catch(err){ res.status(500).json({message:err.message}) } }) // 2. add new data router.post('/',async(req,res)=>{ try{ const newStudent=await StudentsModel.create(req.body) res.status(201).json(newStudent) }catch(err){ res.status(400).json({message:err.message}) } }) // 4. update All data // { new: true} para latest value show krta h router.put('/:id',async(req,res)=>{ try{ const updateStudent=await StudentsModel.findByIdAndUpdate(req.params.id,req.body,{ new: true}) if(!updateStudent) return res.status(404).json({message:'students not found'}) res.json(updateStudent) }catch(err){ res.status(500).json({message:err.message}) } }) // 5. Delete data router.delete('/:id',async(req,res)=>{ try{ const delstudent=await StudentsModel.findByIdAndDelete(req.params.id) if(!delstudent) return res.status(404).json({message:'students not found'}) res.json( {message:'student Deleted'}) }catch(err){ res.status(500).json({message:err.message}) } }) //module export route in index.js files and import in index.js module.exports=router package.json "dotenv": "^16.5.0", "express": "^5.1.0", "mongoose": "^8.15.1", "nodemon": "^3.1.10"
June-06-2025 21:23:08
June-06-2025 11:35:05
June-06-2025 08:25:42
June-02-2025 21:09:41
June-02-2025 10:56:22
June-02-2025 10:27:58
June-02-2025 10:15:40
May-30-2025 19:20:40
May-30-2025 19:14:29
May-27-2025 21:52:18
May-27-2025 21:33:30
May-26-2025 17:11:41
May-26-2025 16:53:29
May-26-2025 16:50:59
May-13-2025 21:02:32
May-13-2025 17:46:23
May-12-2025 15:41:29