<?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: atul thakre</title>
    <description>The latest articles on DEV Community by atul thakre (@atul_thakre_a3ed87610e45f).</description>
    <link>https://dev.to/atul_thakre_a3ed87610e45f</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%2F1588119%2F0192a0c3-e924-456b-86ae-13236b4645e0.jpg</url>
      <title>DEV Community: atul thakre</title>
      <link>https://dev.to/atul_thakre_a3ed87610e45f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atul_thakre_a3ed87610e45f"/>
    <language>en</language>
    <item>
      <title>6 Essential Express Request Properties Every Developer Should Know</title>
      <dc:creator>atul thakre</dc:creator>
      <pubDate>Thu, 12 Sep 2024 12:24:30 +0000</pubDate>
      <link>https://dev.to/atul_thakre_a3ed87610e45f/6-essential-express-request-properties-every-developer-should-know-3ig7</link>
      <guid>https://dev.to/atul_thakre_a3ed87610e45f/6-essential-express-request-properties-every-developer-should-know-3ig7</guid>
      <description>&lt;p&gt;When working on the backend of projects, handling requests and responses is crucial. Managing these requests efficiently is essential for smooth communication between the client and server. Here are some common and important request properties that every developer should be familiar with.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. req.ip
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;req.ip&lt;/code&gt; in Express.js is a property of the request object that provides the IP address of the client making the request. It returns a string representing the client's IP address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling Proxies:&lt;/strong&gt;&lt;br&gt;
   By default, if the app is behind a proxy, req.ip might return the IP of the proxy rather than the real client's IP. To get the correct client IP, you can configure Express to trust the proxy by setting trust proxy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.set("trust proxy", true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, req.ip will return the correct IP of the client even when behind a proxy.&lt;/p&gt;

&lt;p&gt;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('/', (req, res) =&amp;gt; {
  console.log(req.ip);  // Logs the client's IP address
  res.send(`Your IP address is ${req.ip}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. req.cookies
&lt;/h2&gt;

&lt;p&gt;To use &lt;code&gt;req.cookies&lt;/code&gt; we need to install the package called &lt;code&gt;cookie-parser&lt;/code&gt; middleware. The property is use to get cookies sent by the client in &lt;code&gt;Cookies&lt;/code&gt; header. &lt;code&gt;Cookies&lt;/code&gt; in the &lt;code&gt;req.cookies&lt;/code&gt; are unsigned, means they are not been tempered with or verified. You can use this for less sensitive data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import cookieParser from "cookie-parser";

app.use(cookieParser());

app.get('/example', (req, res) =&amp;gt; {
  console.log(req.cookies);  // { cookieName: 'cookieValue' }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. req.signedCookies
&lt;/h2&gt;

&lt;p&gt;To use &lt;code&gt;req.signedCookies&lt;/code&gt; we need to install the package called &lt;code&gt;cookie-parser&lt;/code&gt; middleware. This contains cookies that have been signed and verified to ensure they haven't been altered. A signed cookies are encrypted with the secret key. The &lt;code&gt;req.signedCookies&lt;/code&gt; store only the cookies which passes verification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Set Signed Cookies:&lt;/strong&gt;&lt;br&gt;
  To set a signed cookie, you can use the &lt;code&gt;signed: true&lt;/code&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res.cookie('signedCookieName', 'signedValue', { signed: true });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example:&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;import cookieParser from "cookie-parser";

app.use(cookieParser('your-secret-key'));

app.get('/example', (req, res) =&amp;gt; {
  console.log(req.signedCookies);  // { signedCookieName: 'signedValue' }
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. req.params
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;req.params&lt;/code&gt; in Express.js is an object that contains route parameters from the URL of the request. Route parameters are named placeholders in the route path, and &lt;code&gt;req.params&lt;/code&gt; stores the values provided by the client in the URL for those placeholders.&lt;br&gt;
 You can define a route with parameters by using a colon (:) before the parameter name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;app.get('/users/:userId', (req, res) =&amp;gt; {
  console.log(req.params); // { userId: 'valueFromURL' }
  res.send(`User ID is ${req.params.userId}`);
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. req.query
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;req.query&lt;/code&gt; in Express.js is an object that contains the query string parameters from the URL of the request. Query strings are key-value pairs that come after the ? in a URL and are used to pass additional data to the server. Multiple queries can be pass using &lt;code&gt;&amp;amp;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://example.com/search?term=flowers&amp;amp;sort=asc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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('/search', (req, res) =&amp;gt; {
  console.log(req.query);  // { term: 'flowers', sort: 'asc' }
  res.send(`Search term: ${req.query.term}, Sort order: ${req.query.sort}`);
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. req.body
&lt;/h2&gt;

&lt;p&gt;It contains key-value pairs of data sent by the client. By default, its value is &lt;code&gt;undefined&lt;/code&gt;. The body data can be populated using built-in middleware such as &lt;code&gt;express.json&lt;/code&gt; and &lt;code&gt;urlencoded({ extended: true })&lt;/code&gt;, or external middleware like body-parser.&lt;/p&gt;

&lt;p&gt;The following code help you to understand&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from "express";

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get("/user", (req, res) =&amp;gt; {
    const user  = req.body;
    consolo.log(user);
    res.send(user);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you found this helpful! Don't forget to like and save for future reference. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
