Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Things have <a href="https://expressjs.com/en/changelog/4x.html#4.16.0" rel="noreferrer">changed</a> once again starting <strong>Express 4.16.0</strong>, you can now use <code>express.json()</code> and <code>express.urlencoded()</code> just like in <strong>Express 3.0</strong>.</p> <p>This was <a href="https://github.com/senchalabs/connect#middleware" rel="noreferrer">different</a> starting <strong>Express 4.0 to 4.15</strong>:</p> <pre><code>$ npm install --save body-parser </code></pre> <p>and then:</p> <pre><code>var bodyParser = require('body-parser') app.use( bodyParser.json() ); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); </code></pre> <p>The rest is like in <strong>Express 3.0</strong>:</p> <p>Firstly you need to add some middleware to parse the post data of the body.</p> <p>Add one or both of the following lines of code:</p> <pre><code>app.use(express.json()); // to support JSON-encoded bodies app.use(express.urlencoded()); // to support URL-encoded bodies </code></pre> <p>Then, in your handler, use the <a href="http://expressjs.com/api.html#req.body" rel="noreferrer"><code>req.body</code></a> object:</p> <pre><code>// assuming POST: name=foo&amp;color=red &lt;-- URL encoding // // or POST: {"name":"foo","color":"red"} &lt;-- JSON encoding app.post('/test-page', function(req, res) { var name = req.body.name, color = req.body.color; // ... }); </code></pre> <hr> <p>Note that the use of <a href="http://expressjs.com/api.html#bodyParser" rel="noreferrer"><code>express.bodyParser()</code></a> is not recommended.</p> <pre><code>app.use(express.bodyParser()); </code></pre> <p>...is equivalent to:</p> <pre><code>app.use(express.json()); app.use(express.urlencoded()); app.use(express.multipart()); </code></pre> <p>Security concerns exist with <code>express.multipart()</code>, and so it is better to explicitly add support for the specific encoding type(s) you require. If you do need multipart encoding (to support uploading files for example) then you should <a href="https://groups.google.com/forum/#!msg/express-js/iP2VyhkypHo/5AXQiYN3RPcJ" rel="noreferrer">read this</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload