<?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: Viraj Nirbhavane</title>
    <description>The latest articles on DEV Community by Viraj Nirbhavane (@viraj28).</description>
    <link>https://dev.to/viraj28</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%2F754422%2Fb8b2930d-ec6b-4b5b-8b9c-2d408b62d25a.jpeg</url>
      <title>DEV Community: Viraj Nirbhavane</title>
      <link>https://dev.to/viraj28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/viraj28"/>
    <language>en</language>
    <item>
      <title>Controllers</title>
      <dc:creator>Viraj Nirbhavane</dc:creator>
      <pubDate>Wed, 16 Mar 2022 11:14:29 +0000</pubDate>
      <link>https://dev.to/viraj28/controllers-3ifh</link>
      <guid>https://dev.to/viraj28/controllers-3ifh</guid>
      <description>&lt;p&gt;A GET request route set up in a seperate route File instead of the server.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** greetingRoute.js */ 
...
router.get('/', (req,res) =&amp;gt; {
   res.status(200).json({ message: "Greetings!"});
});
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could set up routes like this, they aren't doing much currently but they are set up. We could proceed to add our functionality in the body of these callback functions but it's much better practice to create a controller and have your functions there.&lt;/p&gt;

&lt;p&gt;So, in the backend folder that we had, create a controllers folder and add a file for the corresponding controller. Lets say, greetingController.js&lt;/p&gt;

&lt;p&gt;Here, we can create some functions. For instance, lets have a getGreeting function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** greetingController.js */

const getGreeting = (req,res) =&amp;gt; {
    res.status(200).json({ message: "Greetings!"});
}

module.exports = {
    getGreeting,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to export it in order to use it in our routes file.&lt;br&gt;
The modules.exports is a Object here because we might wanna add more functions to it later as routes grow.&lt;/p&gt;

&lt;p&gt;So, now in our routes file we need to require it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** greetingRoute.js */
...
const { getGreeting } = require('../controllers/greetingController.js');
...

router.get('/', getGreeting);

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

&lt;/div&gt;



&lt;p&gt;Notice how we replaced the callback function body with the getGreeting function that we pulled in via require above.&lt;br&gt;
And it should still work!&lt;/p&gt;

&lt;p&gt;You wanna do these with rest of the routes as well and clear the clutter from the routes file as well.&lt;/p&gt;

&lt;p&gt;And when you are done with all the routes, you can chain same routes but different request types together, like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

router.route('/').get(getGreeting).post(setGreeting);
router.route('/:id').update(updateEntity).delete(deleteEntity);

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

&lt;/div&gt;



&lt;p&gt;That's it for this one. Leave a like!&lt;/p&gt;

</description>
      <category>node</category>
      <category>api</category>
      <category>express</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Routes</title>
      <dc:creator>Viraj Nirbhavane</dc:creator>
      <pubDate>Tue, 15 Mar 2022 10:32:10 +0000</pubDate>
      <link>https://dev.to/viraj28/routes-490c</link>
      <guid>https://dev.to/viraj28/routes-490c</guid>
      <description>&lt;p&gt;A route is a code that represents an HTTP action, &lt;em&gt;(GET, POST, PUT, DELETE)&lt;/em&gt;. It has a URL endpoint, and a function that is used to handle when that endpoint is accessed.&lt;/p&gt;

&lt;p&gt;We use postman to test these endpoints. Accessing these endpoints prior to setting up the code will return a 404 NOT FOUND - a html response.&lt;/p&gt;

&lt;p&gt;Lets look at the code for a GET request at greeting endpoint.&lt;br&gt;
We call the get method on the app object, passing in two arguments&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Endpoint.&lt;/li&gt;
&lt;li&gt;The function to handle this endpoint. It takes in a request and a response object as parameters.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/api/greeting', (req, res) =&amp;gt; {
    res.send('Hello there, Welcome!')
} )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, if we access the route, we get a 200 OK status and the String "Hello there...." is returned.&lt;/p&gt;

&lt;p&gt;Usually we are gonna return JSON data. So the above code could be changed to&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('/api/greeting', (req, res) =&amp;gt; {
    res.status(200).json({message:'Hello there, Welcome!'})
} )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also set the status, although not necessary. Now we respond with a JSON object and the content type of the response changes from html to application/json. Even though we didn't add quotes on the key "message", it get parsed into JSON. &lt;/p&gt;




&lt;p&gt;Now we don't wanna clutter our server.js file with all these routes. So we clean it up.&lt;/p&gt;

&lt;p&gt;In the backend folder, we create a folder called 'routes' and in it we create file greetRoutes.js. So basically each resource in API will have it's own route file.&lt;/p&gt;

&lt;p&gt;Folder structure : backend/routes/greetRoutes.js&lt;/p&gt;

&lt;p&gt;Now to use the express router, we are gonna first bring the express into this file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#greetRoutes.js

const express = require('express');
const router = express.Router();

router.get('/', (req, res) =&amp;gt; {
    res.status(200).json({message:'Hello there, Welcome!'})
} )

module.exports = router

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

&lt;/div&gt;



&lt;p&gt;and inside server.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#server.js
....
app.use('/api/greeting', require('./routes/greetRoutes.js');
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, if we hit /api/greeting, it's gonna look into the Route file, and all we needed was a / because /api/greeting is already specified.&lt;/p&gt;

&lt;p&gt;Similarly, we can create other routes for POST, PUT, DELETE.&lt;br&gt;
Some of them will require a :id in the endpoint part so that they access the right data point. &lt;code&gt;router.put('/:id', (req,res) =&amp;gt; {....})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next up, Controllers. Again to clear the clutter!&lt;/p&gt;

</description>
      <category>api</category>
      <category>routes</category>
      <category>expressrouter</category>
    </item>
    <item>
      <title>Express Server</title>
      <dc:creator>Viraj Nirbhavane</dc:creator>
      <pubDate>Tue, 15 Mar 2022 07:29:19 +0000</pubDate>
      <link>https://dev.to/viraj28/express-server-4j86</link>
      <guid>https://dev.to/viraj28/express-server-4j86</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt; : Node, npm&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;npm init&lt;/code&gt; in the root folder. Fill up the details and it will generate a package.json for you. You may change the entry point to server.js, optional. Create a folder "backend" and inside it a file "server.js"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit the package.json file and add in the scripts object, the following:&lt;br&gt;
&lt;code&gt;"start": "node backend/server.js",&lt;br&gt;
"server": "nodemon backend/server.js"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the dependencies: &lt;br&gt;
&lt;code&gt;npm i express dotenv&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm i -D nodemon&lt;/code&gt; -D implies that it is a devDependency.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Lets Edit the server.js file now to spin up a server.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PB14Tb96--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs9knb7m8bjfchxnifyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PB14Tb96--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs9knb7m8bjfchxnifyp.png" alt="nodejs code for express server" width="644" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bring in express and other stuff by requiring it at the top.&lt;/li&gt;
&lt;li&gt;dotenv is used so that we can use  Environment variables.&lt;/li&gt;
&lt;li&gt;Specify the &lt;code&gt;PORT=5000&lt;/code&gt; in a .env file, that way we can acces it in the code like &lt;code&gt;process.env.PORT&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Make a instance of express, using its constructor and assign it to app&lt;/li&gt;
&lt;li&gt;Call the listen method on app object and pass in the port and the callback&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, you can fire it up by running the command &lt;code&gt;npm run server&lt;/code&gt;, this way nodemon will watch our files for changes and server will be up and running.&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>api</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
