Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sinha express crud template

async function run(){
    try{
        await client.connect();
        const userCollection = client.db('foodExpress').collection('user');

        // get users
        app.get('/user', async(req, res) =>{
            const query = {};
            const cursor = userCollection.find(query);
            const users = await cursor.toArray();
            res.send(users);
        });

        app.get('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const query = {_id: ObjectId(id)};
            const result = await userCollection.findOne(query);
            res.send(result);
        });

        // POST User : add a new user
        app.post('/user', async(req, res) =>{
            const newUser = req.body;
            console.log('adding new user', newUser);
            const result = await userCollection.insertOne(newUser);
            res.send(result)
        });

        // update user
        app.put('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const updatedUser = req.body;
            const filter = {_id: ObjectId(id)};
            const options = { upsert: true };
            const updatedDoc = {
                $set: {
                    name: updatedUser.name,
                    email: updatedUser.email
                }
            };
            const result = await userCollection.updateOne(filter, updatedDoc, options);
            res.send(result);

        })

        // delete a user
        app.delete('/user/:id', async(req, res) =>{
            const id = req.params.id;
            const query = {_id: ObjectId(id)};
            const result = await userCollection.deleteOne(query);
            res.send(result);
        })

    }
    finally{

    }
}

run().catch(console.dir);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript variable scope in if statement 
Javascript :: Backbone Model Validation And Inheritance 
Javascript :: recursive function and json object 
Javascript :: how to set javascript load order in html 
Javascript :: discord.js create a private channel 
Javascript :: Check If Backbone Model Has Property 
Javascript :: base64-XMLHttpRequest 
Javascript :: Backbone Template Simple Example 
Javascript :: chart cdn js 
Javascript :: Solution-4--solution options for reverse bits algorithm js 
Javascript :: array of objects javascript 
Javascript :: var date = new Date(); 
Javascript :: inline css in react js 
Javascript :: arguments object 
Javascript :: how to generate random ip address in javascript 
Javascript :: react native new project 
Javascript :: regex in javascript 
Javascript :: switch statement javascript 
Javascript :: yarn react 
Javascript :: color switcher 
Javascript :: assing multipe ids jquery to event 
Javascript :: vue__WEBPACK_IMPORTED_MODULE_0__.reactive) 
Javascript :: javascript Adding Properties And Methods in an Object 
Javascript :: JavaScript Destructuring - Before ES6 
Javascript :: JavaScript Code Blocks 
Javascript :: simple counter with react hook 
Javascript :: get data notifacation realtime use ajax good 
Javascript :: change x scale phaser 
Javascript :: phaser create animation on sprite 
Javascript :: Who likes it 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =