<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: aktarina akhi</title>
    <description>The latest articles on DEV Community by aktarina akhi (@akhi).</description>
    <link>https://dev.to/akhi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F717406%2F2e937264-b63e-4c83-aeb2-df458bab9c99.jpeg</url>
      <title>DEV Community: aktarina akhi</title>
      <link>https://dev.to/akhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akhi"/>
    <language>en</language>
    <item>
      <title>CRUD operation using node</title>
      <dc:creator>aktarina akhi</dc:creator>
      <pubDate>Thu, 23 Dec 2021 17:01:00 +0000</pubDate>
      <link>https://dev.to/akhi/crud-operation-using-node-gog</link>
      <guid>https://dev.to/akhi/crud-operation-using-node-gog</guid>
      <description>&lt;p&gt;&lt;strong&gt;### CRUD Operations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CRUD (Create, Read, Update, Delete) is an acronym for ways one can operate on stored data. It refers to operations performed in a database.&lt;/p&gt;

&lt;p&gt;There is some HTTP method that is used to operate CRUD operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POST Method :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The POST method is used to create subordinate or new resources. When creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/products', async (req, res) =&amp;gt; {
            const product = req.body;
            console.log(product)
            const result = await productsCollection.insertOne(product);
            res.json(result);
        });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GET Method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The HTTP GET method is used to read a representation of a resource and returns a representation in XML or JSON and an HTTP response code of 200 (OK).&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  app.get('/products', async (req, res) =&amp;gt; {
            const cursor = productsCollection.find({})
            const products = await cursor.toArray();
            res.json(products);
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PUT Method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PUT is used for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; app.put('/users', async (req, res) =&amp;gt; {
            const user = req.body;
            const filter = { email: user.email }
            const options = { upsert: true }
            const updateDoc = { $set: user }
            const result = await usersCollection.updateOne(filter, updateDoc, options)
            res.json(result);
        });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PATCH Method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PATCH is used to modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PATCH /user/jthijssen HTTP/1.1
&amp;lt;user&amp;gt;
    &amp;lt;firstname&amp;gt;Joshua&amp;lt;/firstname&amp;gt;
&amp;lt;/user&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DELETE Method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DELETE is used to delete a resource identified by a URI.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; app.delete('/orders/:id', async (req, res) =&amp;gt; {
            const id = req.params.id;
            const query = { _id: ObjectId(id) }
            const result = await ordersCollection.deleteOne(query);
            res.json(result);
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To operate this operation, you have to must install,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm i cors&lt;/p&gt;

&lt;p&gt;npm i mongodb&lt;/p&gt;

&lt;p&gt;npm i express&lt;/p&gt;

&lt;p&gt;npm i dotenv&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And must update in script object, &lt;br&gt;
like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "start": "node index.js",
    "start-dev": "nodemon index.js",
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
