0% found this document useful (0 votes)
2 views5 pages

TP 04 Mongo DB

The document outlines the steps to create a Node.js project using Express and MongoDB, including initializing the project, setting up a server, and handling CRUD operations for a 'clients' collection. It provides code snippets for connecting to MongoDB and defining API endpoints for retrieving, adding, updating, and deleting client data. Additionally, it mentions common errors and solutions related to MongoDB installation.

Uploaded by

hamidanh855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

TP 04 Mongo DB

The document outlines the steps to create a Node.js project using Express and MongoDB, including initializing the project, setting up a server, and handling CRUD operations for a 'clients' collection. It provides code snippets for connecting to MongoDB and defining API endpoints for retrieving, adding, updating, and deleting client data. Additionally, it mentions common errors and solutions related to MongoDB installation.

Uploaded by

hamidanh855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tp :mongodb ou mongoose et node js

1- Créer un projet NODE JS taper la commande npm init


2- Ajouter le fichier index.js
3- Installer et créer un serveur ExpressJS taper la commande npm i express
4- Creation de server nodemon npm i nodemon
5- npm install mongodb
6- Les erreurs :
7- npm uninstall mongodb
8- npm install [email protected]

Index.js

const express= require('express');

const app= express()

app.listen(300,()=>{

console.log('notre server express bien demarer')

})

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://localhost:27017';

const dbName = 'fs206';

let db

MongoClient.connect(url, function(err, client) {

console.log("Connexion réussi avec Mongo");

db = client.db(dbName);

});

app.get('/clients',(req,res)=>{

db.collection('etudiants').find({}).toArray(function(err,docs){

if(err){ console.log(err)}

res.status(200).json(docs)

})
})

app.get('/clients/:id',async (req,res)=>{

const id =parseInt( req.params.id)

try {

const docs = await db.collection('clients').find({id}).toArray()

res.status(200).json(docs)

} catch (err) {

console.log(err)

throw err

})

app.post('/clients', async (req,res) => {

try {

const collClient = req.body

const client = await db.collection('clients').insertOne(collClient )

res.status(200).json(client)

} catch (err) {

console.log(err)

throw err

})

app.put('/clients/:id', async (req,res) => {

try {

const id = parseInt(req.params.id)

const clientMOf = req.body

const client = await db.collection('clients').replaceOne({id},clientMOf)

res.status(200).json(client)

} catch (err) {

console.log(err)
throw err

})

app.delete('/clients/:id', async (req,res) => {

try {

const id = parseInt(req.params.id)

const client = await db.collection('clients').deleteOne({id})

res.status(200).json(client)

} catch (err) {

console.log(err)

throw err

})

/*

app.get('/clients',(req,res)=>{

// res.send(Lclients);

res.status(200).json(Lclients)

})

app.get('/clients/:id',(req,res)=>{

// res.send(Lclients);

const id=req.params.id

const cl=Lclients.find(cl=>cl.id==id)

res.status(200).json(cl)

})

app.post('/clients',(req,res)=>{

Lclients.push(req.body);

res.status(200).json(Lclients)

})
app.put('/clients/:id',(req,res)=>{

const id=req.params.id

const cl=Lclients.find(cl=>cl.id==id)

cl.nom=req.body.nom;

cl.ville=req.body.ville;

cl.age=req.body.age;

res.status(200).json(Lclients)

})

app.delete('/clients/:id',(req,res)=>{

const id=req.params.id

const cl=Lclients.find(cl=>cl.id==id)

Lclients.splice(Lclients.indexOf(cl),1);

res.status(200).json(Lclients)

})*/

Les erreurs :
npm uninstall mongodb
npm install [email protected]

You might also like