<?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: Pete Griffith</title>
    <description>The latest articles on DEV Community by Pete Griffith (@griffitp12).</description>
    <link>https://dev.to/griffitp12</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%2F628132%2Fef04dc12-7ad8-4029-ad90-2e58cfa8e947.jpeg</url>
      <title>DEV Community: Pete Griffith</title>
      <link>https://dev.to/griffitp12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/griffitp12"/>
    <language>en</language>
    <item>
      <title>Express's json() and urlencoded() Explained</title>
      <dc:creator>Pete Griffith</dc:creator>
      <pubDate>Sun, 09 May 2021 03:02:10 +0000</pubDate>
      <link>https://dev.to/griffitp12/express-s-json-and-urlencoded-explained-1m7o</link>
      <guid>https://dev.to/griffitp12/express-s-json-and-urlencoded-explained-1m7o</guid>
      <description>&lt;p&gt;Express is a Node.js framework best known by beginners for simplifying syntax in building basic back-end servers. Express.js comes with a lot of built in modules and functions, and we're going to talk about two of them specifically today: .json() and .urlencoded(). &lt;/p&gt;

&lt;p&gt;.json() and .urlencoded() are express middleware functions that parse data in outgoing requests. Let's break that down: &lt;/p&gt;

&lt;h3&gt;
  
  
  1.) They are middleware functions
&lt;/h3&gt;

&lt;p&gt;So what is middleware?&lt;/p&gt;

&lt;p&gt;Middleware is a function or piece of code that is called (run) between when a server gets a request from a client and when it sends a response back to the client. Middleware can take many forms, like simple logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('server listening on port ${port})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or functions that manipulate incoming or outgoing data (such as &lt;code&gt;.json()&lt;/code&gt; and &lt;code&gt;.urlencoded()&lt;/code&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  2.) They parse outgoing requsts
&lt;/h3&gt;

&lt;p&gt;Both are used to parse data that goes out on the &lt;code&gt;req&lt;/code&gt; object. Usage of .json() and .urlencoded() depends on the encoding of data you are sending in your server request.&lt;/p&gt;

&lt;p&gt;Because they are used on the &lt;code&gt;req&lt;/code&gt; side of a server call, you only need them for POST and PUT requests, or any outgoing request that carries data. They aren't needed for GET or DELETE requests.&lt;/p&gt;

&lt;p&gt;Incidently, both used to be part of &lt;code&gt;body-parser&lt;/code&gt; which was in itself a piece of middleware that was taken out and separated from Express, but since Express 4.16.0 they're back into Express and use the &lt;code&gt;express.FUNCTIONNAME()&lt;/code&gt; syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what exactly is the difference and how can I use them?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;.json()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Express.json() expects request data to be sent in JSON format, which often resembles a simple JS object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"Name": "Pikachu", "Type": "Banana", "Number In Stable": 12}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;.urlencoded()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Express.urlencoded() expects request data to be sent encoded in the URL, usually in strings or arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.../Name=Pikachu&amp;amp;Type=Banana&amp;amp;Number+In+Stable=12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  You can call them using
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;express.json()

or

express.urlencoded()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but if you want them to be used every time a request is made on the server (which is a typical use case), you can also use &lt;code&gt;app.use&lt;/code&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.use(express.json())

or 

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;express.json()&lt;/code&gt; and &lt;code&gt;express.urlencoded()&lt;/code&gt; are helpful express middleware parser functions that let you parse outgoing request data depending on the encoding of data you're sending to the server.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
