Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

express get, post, delete, put 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 :: how to create existing nodes in godot 
Javascript :: Backbone Model Setting, Has And Getting 
Javascript :: JavaScript is synchronous by default 
Javascript :: Update react final form field 
Javascript :: remove T from datetime in js 
Javascript :: dockerignore node_modules 
Javascript :: convert milliseconds to seconds javascript 
Javascript :: iconbuttons onclick redirect to another page on react 
Javascript :: adding javascript object within array in the middle position 
Javascript :: Solution-2--solution options for reverse bits algorithm js 
Javascript :: how to receive form data in node js 
Javascript :: convert text to number 
Javascript :: react tutorial app 
Javascript :: timezone using javascript 
Javascript :: how to define class in javascript 
Javascript :: react onsubmit get form values 
Javascript :: ~~ in js 
Javascript :: fill array with array javascript 
Javascript :: react component pass props 
Javascript :: javascript timer countdown with seconds 59 
Javascript :: knockout subscribe 
Javascript :: p cannot appear as a descendant of p react 
Javascript :: javascript Create Objects: Constructor Function Vs Object Literal 
Javascript :: javascript Set Subset Operation 
Javascript :: javascript statements 
Javascript :: node js - excecute a child process and exchange message to and from 
Javascript :: mongoose findbyidandupdate or findoneandupdate 
Javascript :: change rotation phaser 
Javascript :: phaser create animation from texture atlas 
Javascript :: Is he gonna survive 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =