• Ulanish kodi
  • Node.js bilan MongoDB ma’lumotlar bazasini bog‘lash




    Download 2,11 Mb.
    Pdf ko'rish
    bet23/28
    Sana23.12.2023
    Hajmi2,11 Mb.
    #127300
    1   ...   20   21   22   23   24   25   26   27   28
    Bog'liq
    MBB Loyiha ishi uslubiy ko`rsatma (2)

    2.3. Node.js bilan MongoDB ma’lumotlar bazasini bog‘lash 
    Node.js 
    yordamida 
    biz 
    cloud.mongodb.com 
    saytidagi 
    shaxsiy 
    kabinetimizdagi ma’lumotlarni olishimiz va u yerdagi o‘zimizning ma’lumotlar 
    bazamizdan istalgan dasturda foydalanishimiz mumkin. Quyida Node.js yordamida 
    MongoDB da yaratgan “Kasalxona” ma’lumotlar bazasiga ulanish kodi keltirilgan: 
     
    2.13-rasm. Node.js orqali MongoDB ga ulanish 
    Ulanish kodi: 
    const express = require("express"); 
    const mongoose = require("mongoose"); 
    const methodOverride = require("method-override"); 
    const bodyParser = require("body-parser"); 
    require("dotenv/config"); 
    const app = express(); 
    const Hodim = require("./models/Hodim"); 
    const Bemor = require("./models/Bemor"); 
    const Dori = require("./models/Dori"); 
    const YetBer = require("./models/YetkazibBeruvchi"); 


    55 
    mongoose.connect(process.env.ConnectionString, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true, 
    useFindAndModify: false, 
    useCreateIndex: true, 
    }); 
    app.disable("x-powered-by"); 
    app.use( 
    bodyParser.json({ 
    limit: "50mb", 
    extended: true, 
    }) 
    ); 
    app.use( 
    bodyParser.urlencoded({ 
    limit: "50mb", 
    extended: true, 
    parameterLimit: 50000, 
    }) 
    ); 
    app.use(function (req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header( 
    "Access-Control-Allow-Headers", 
    "Origin, X-Requested-With, Content-Type, Accept" 
    ); 
    res.header("Access-Control-Expose-Headers", "x-token"); 
    next(); 
    }); 
    app.set("view engine", "ejs"); 
    app.use(express.urlencoded({ extended: false })); 
    app.use(methodOverride("_method")); 
    // asosiy routelar 
    app.get("/", (req, res, next) => { 
    res.render("index"); 
    }); 
    app.get("/hodimlar", async (req, res) => { 
    const hodimlar = await Hodim.find().lean(); 
    res.render("hodimlar", { hodimlar: hodimlar }); 
    }); 
    app.post("/hodimlar", async (req, res, next) => { 
    const hodim = await Hodim.create(req.body); 
    res.status(200).json({ 
    success: true, 


    56 
    data: xodim, 
    }); 
    }); 
    app.get("/bemorlar", async (req, res) => { 
    const bemorlar = await Bemor.find() 
    .populate([{ path: "doktori" }, { path: "dorilar" }]) 
    .lean(); 
    res.render("bemorlar", { bemorlar: bemorlar }); 
    }); 
    app.post("/bemorlar", async (req, res, next) => { 
    const bemor = await Bemor.create(req.body); 
    res.status(200).json({ 
    success: true, 
    data: bemor, 
    }); 
    }); 
    app.get("/dorilar", async (req, res) => { 
    const dorilar = await Dori.find().lean(); 
    res.render("dorilar", { dorilar: dorilar }); 
    }); 
    app.post("/dorilar", async (req, res, next) => { 
    const dori = await Dori.create(req.body); 
    res.status(200).json({ 
    success: true, 
    data: dori, 
    }); 
    }); 
    app.get("/yetberlar", async (req, res) => { 
    const yetberlar = await YetBer.find().lean(); 
    res.render("yetberlar", { yetberlar: yetberlar }); 
    }); 
    app.post("/yetberlar", async (req, res, next) => { 
    const yetber = await YetBer.create(req.body); 
    res.status(200).json({ 
    success: true, 
    data: yetber, 
    }); 
    }); 
    app.listen(5000); 

    Download 2,11 Mb.
    1   ...   20   21   22   23   24   25   26   27   28




    Download 2,11 Mb.
    Pdf ko'rish

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Node.js bilan MongoDB ma’lumotlar bazasini bog‘lash

    Download 2,11 Mb.
    Pdf ko'rish