<?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>Forem: UiX Phuke</title>
    <description>The latest articles on Forem by UiX Phuke (@uix-phuke).</description>
    <link>https://forem.com/uix-phuke</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%2F1724284%2Fe4f4f50e-d837-49c6-bb00-23a1c0bced71.png</url>
      <title>Forem: UiX Phuke</title>
      <link>https://forem.com/uix-phuke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/uix-phuke"/>
    <language>en</language>
    <item>
      <title>Express.js</title>
      <dc:creator>UiX Phuke</dc:creator>
      <pubDate>Mon, 05 Aug 2024 23:20:01 +0000</pubDate>
      <link>https://forem.com/uix-phuke/expressjs-mg</link>
      <guid>https://forem.com/uix-phuke/expressjs-mg</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is Express?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Express.js, or simply Express, is a back end web application framework for building RESTful APIs with Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialisation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;installation of express:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express --save 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;installation of nodemon&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.&lt;/p&gt;

&lt;p&gt;Run express server on: &lt;code&gt;localhost:3000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3ka26no8buhm0vpftf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3ka26no8buhm0vpftf6.png" alt="Image description" width="800" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;send json: &lt;code&gt;localhost:3000/jsonexample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0zpu2yurwq2glo71r8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0zpu2yurwq2glo71r8y.png" alt="Image description" width="800" height="553"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;path is a inbuild module&lt;/em&gt;&lt;br&gt;
&lt;em&gt;we can call directories&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwwg86xc4iw4dyx9uioo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwwg86xc4iw4dyx9uioo.png" alt="Image description" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;params&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The req.params property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /student/:id, then the “id” property is available as req.params.id. This object defaults to {}.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;http://localhost:3000/api/products/3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyfyixy06vh2jem1xet1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyfyixy06vh2jem1xet1.png" alt="Image description" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;query&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;The req.query property allows you to access the query parameters from the URL of an incoming HTTP request. Query parameters are key-value pairs included in the URL after the “?” symbol, and they are separated by “&amp;amp;” symbols.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;http://localhost:3000/api/v1/query?search=r&amp;amp;limit=2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72wzjfgersj1ug0t4gd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72wzjfgersj1ug0t4gd.png" alt="Image description" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic crud operation&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;modules&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const app = express();

//app.use(express.json());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;let books is a arr obj&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let books = [
    {id:1,title:'Book 1',author: 'author 1'},
    {id:2,title:'Book 2',author: 'author 2'},
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;read&lt;/em&gt;&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('/', (req, res) =&amp;gt; {
    res.json(books)
})

app.use(express.json())

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;create&lt;/em&gt;&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('/books', (req, res) =&amp;gt; {
    console.log(req.body)
    const newBook = req.body;
    newBook.id = books.length + 1;
    books.push(newBook);
    res.status(201).json(newBook)

})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;update/put&lt;/em&gt;&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('/books/:id',(req, res)=&amp;gt; {
    const id = parseInt(req.params.id)
    const updatedBooks = req.body
    const index = books.findIndex(book =&amp;gt; book.id === id);

    if (index !== -1) {
        books[index] = { ...books[index], ...updatedBooks }
        res.json(books[index])
    }
    else {
        res.status(404).json({error: "book not found"})
    }

})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;delete&lt;/em&gt;&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('/books/:id', (req, res) =&amp;gt; {
    const id = parseInt(req.params.id);
    const index = books.findIndex((book) =&amp;gt; book.id === id);
    if (index !== -1) {
      const deletedBook = books[index];
      books.slice(index, 1);
      res.json(deletedBook);
    } else {
      res.status(404).json({ error: "book not found" });
    }
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;port&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(3000, () =&amp;gt; {
    console.log('server is running on port 3000')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;but this is not a good way to operate CRUD operations&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;there is a better way&lt;/em&gt;&lt;br&gt;
folder structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75ce90kgr1t7r34pqxms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75ce90kgr1t7r34pqxms.png" alt="Image description" width="223" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const app = express();
const bookRoutes=require('./routes/bookRoutes')

app.use(express.json());
app.use('/',bookRoutes)

app.listen(3000, () =&amp;gt; {
    console.log('server is running on port 3000')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bookController.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let books = [
  { id: 1, title: "Book 1", author: "author 1" },
  { id: 2, title: "Book 2", author: "author 2" },
];

//read
const getBooks=(req, res) =&amp;gt; {
  res.json(books);
}

//create
const createBooks= (req, res) =&amp;gt; {
  console.log(req.body);
  const newBook = req.body;
  newBook.id = books.length + 1;
  books.push(newBook);
  res.status(201).json(newBook);
}

//update /put
const updateBooks= (req, res) =&amp;gt; {
  const id = parseInt(req.params.id);
  const updatedBooks = req.body;
  const index = books.findIndex((book) =&amp;gt; book.id === id);

  if (index !== -1) {
    books[index] = { ...books[index], ...updatedBooks };
    res.json(books[index]);
  } else {
    res.status(404).json({ error: "book not found" });
  }
}

//delete
const deleteBooks=  (req, res) =&amp;gt; {
  const id = parseInt(req.params.id);
  const index = books.findIndex((book) =&amp;gt; book.id === id);
  if (index !== -1) {
    const deletedBook = books[index];
    books.slice(index, 1);
    res.json(deletedBook);
  } else {
    res.status(404).json({ error: "book not found" });
  }
}

module.exports = {
    getBooks,createBooks,updateBooks,deleteBooks
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bookRoutes.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')

const bookController = require('../controllers/bookController')

const router = express.Router()

router.get('/books',bookController.getBooks)
router.post('/books',bookController.createBooks)
router.put('/books/:id',bookController.updateBooks)
router.delete('/books/:id', bookController.deleteBooks)

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;check out in github: &lt;a href="https://github.com/Rohangith1/express" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>express</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Express.js</title>
      <dc:creator>UiX Phuke</dc:creator>
      <pubDate>Mon, 05 Aug 2024 23:19:57 +0000</pubDate>
      <link>https://forem.com/uix-phuke/expressjs-107i</link>
      <guid>https://forem.com/uix-phuke/expressjs-107i</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is Express?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Express.js, or simply Express, is a back end web application framework for building RESTful APIs with Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialisation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;installation of express:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express --save 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;installation of nodemon&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.&lt;/p&gt;

&lt;p&gt;Run express server on: &lt;code&gt;localhost:3000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3ka26no8buhm0vpftf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3ka26no8buhm0vpftf6.png" alt="Image description" width="800" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;send json: &lt;code&gt;localhost:3000/jsonexample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0zpu2yurwq2glo71r8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0zpu2yurwq2glo71r8y.png" alt="Image description" width="800" height="553"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;path is a inbuild module&lt;/em&gt;&lt;br&gt;
&lt;em&gt;we can call directories&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwwg86xc4iw4dyx9uioo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpwwg86xc4iw4dyx9uioo.png" alt="Image description" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;params&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The req.params property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /student/:id, then the “id” property is available as req.params.id. This object defaults to {}.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;http://localhost:3000/api/products/3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyfyixy06vh2jem1xet1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyfyixy06vh2jem1xet1.png" alt="Image description" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;query&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;The req.query property allows you to access the query parameters from the URL of an incoming HTTP request. Query parameters are key-value pairs included in the URL after the “?” symbol, and they are separated by “&amp;amp;” symbols.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;http://localhost:3000/api/v1/query?search=r&amp;amp;limit=2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72wzjfgersj1ug0t4gd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72wzjfgersj1ug0t4gd.png" alt="Image description" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic crud operation&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;modules&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const app = express();

//app.use(express.json());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;let books is a arr obj&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let books = [
    {id:1,title:'Book 1',author: 'author 1'},
    {id:2,title:'Book 2',author: 'author 2'},
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;read&lt;/em&gt;&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('/', (req, res) =&amp;gt; {
    res.json(books)
})

app.use(express.json())

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;create&lt;/em&gt;&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('/books', (req, res) =&amp;gt; {
    console.log(req.body)
    const newBook = req.body;
    newBook.id = books.length + 1;
    books.push(newBook);
    res.status(201).json(newBook)

})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;update/put&lt;/em&gt;&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('/books/:id',(req, res)=&amp;gt; {
    const id = parseInt(req.params.id)
    const updatedBooks = req.body
    const index = books.findIndex(book =&amp;gt; book.id === id);

    if (index !== -1) {
        books[index] = { ...books[index], ...updatedBooks }
        res.json(books[index])
    }
    else {
        res.status(404).json({error: "book not found"})
    }

})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;delete&lt;/em&gt;&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('/books/:id', (req, res) =&amp;gt; {
    const id = parseInt(req.params.id);
    const index = books.findIndex((book) =&amp;gt; book.id === id);
    if (index !== -1) {
      const deletedBook = books[index];
      books.slice(index, 1);
      res.json(deletedBook);
    } else {
      res.status(404).json({ error: "book not found" });
    }
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;port&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(3000, () =&amp;gt; {
    console.log('server is running on port 3000')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;but this is not a good way to operate CRUD operations&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;there is a better way&lt;/em&gt;&lt;br&gt;
folder structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75ce90kgr1t7r34pqxms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75ce90kgr1t7r34pqxms.png" alt="Image description" width="223" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const app = express();
const bookRoutes=require('./routes/bookRoutes')

app.use(express.json());
app.use('/',bookRoutes)

app.listen(3000, () =&amp;gt; {
    console.log('server is running on port 3000')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bookController.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let books = [
  { id: 1, title: "Book 1", author: "author 1" },
  { id: 2, title: "Book 2", author: "author 2" },
];

//read
const getBooks=(req, res) =&amp;gt; {
  res.json(books);
}

//create
const createBooks= (req, res) =&amp;gt; {
  console.log(req.body);
  const newBook = req.body;
  newBook.id = books.length + 1;
  books.push(newBook);
  res.status(201).json(newBook);
}

//update /put
const updateBooks= (req, res) =&amp;gt; {
  const id = parseInt(req.params.id);
  const updatedBooks = req.body;
  const index = books.findIndex((book) =&amp;gt; book.id === id);

  if (index !== -1) {
    books[index] = { ...books[index], ...updatedBooks };
    res.json(books[index]);
  } else {
    res.status(404).json({ error: "book not found" });
  }
}

//delete
const deleteBooks=  (req, res) =&amp;gt; {
  const id = parseInt(req.params.id);
  const index = books.findIndex((book) =&amp;gt; book.id === id);
  if (index !== -1) {
    const deletedBook = books[index];
    books.slice(index, 1);
    res.json(deletedBook);
  } else {
    res.status(404).json({ error: "book not found" });
  }
}

module.exports = {
    getBooks,createBooks,updateBooks,deleteBooks
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bookRoutes.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')

const bookController = require('../controllers/bookController')

const router = express.Router()

router.get('/books',bookController.getBooks)
router.post('/books',bookController.createBooks)
router.put('/books/:id',bookController.updateBooks)
router.delete('/books/:id', bookController.deleteBooks)

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/Rohangith1/express" rel="noopener noreferrer"&gt;check out in github:&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>express</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>About Driver.js</title>
      <dc:creator>UiX Phuke</dc:creator>
      <pubDate>Thu, 18 Jul 2024 08:25:51 +0000</pubDate>
      <link>https://forem.com/uix-phuke/about-driverjs-2pk6</link>
      <guid>https://forem.com/uix-phuke/about-driverjs-2pk6</guid>
      <description>&lt;p&gt;TLDRDriver.js is a lightweight, highly customizable, and dependency-free JavaScript library designed to support all major browsers and mobile devices. It helps in user onboarding, feature highlighting, and providing contextual help. Written in TypeScript and MIT Licensed, it has millions of downloads and is used globally by various companies.&lt;/p&gt;

&lt;p&gt;Installation: &lt;br&gt;
Run one of the following commands to install the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Using npm
npm install driver.js

# Using pnpm
pnpm install driver.js

# Using yarn
yarn add driver.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>MCA or SAAS Product by feb 2025</title>
      <dc:creator>UiX Phuke</dc:creator>
      <pubDate>Wed, 17 Jul 2024 12:23:33 +0000</pubDate>
      <link>https://forem.com/uix-phuke/mca-or-saas-product-by-feb-2025-247p</link>
      <guid>https://forem.com/uix-phuke/mca-or-saas-product-by-feb-2025-247p</guid>
      <description>&lt;p&gt;Hey there…&lt;br&gt;
I know that I’m new to you guys…. &lt;br&gt;
I think that i’m taking a big risk in my life… in 2024 I graduated CS degree with 8.21 CGPA. But what matters … this cgpa not gonna help me anyway … during this degree i sidly associated with web dev… due to lack of consistency… i think i could not made it as i want … so, I’m thinking that rather go for MCA and wasting money… may i become a self-taught MERN stack developer by December 2024. &lt;/p&gt;

&lt;p&gt;And build a SAAS product …. And also some full stack project.. so company gets to know my skill… and if i get internship somewhere!!!&lt;/p&gt;

&lt;p&gt;What u say… &lt;br&gt;
Should i go gor MCA&lt;br&gt;
or a build a SAAS product which may implement by feb 2025.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>mern</category>
      <category>saas</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
