So now we have a handler for
/create-post. Next step is to actually find our blog post.
The contents of your blog post is hidden in your
req object somewhere. Normally you would extract it using
req.body. Try to console.log
undefined? Not to worry, that's normal. When data has been
POSTed to the server as
FormData, we need to do things slightly differently to access the data that's come through in the request.
We need another middleware function. Something that can get extract the contents out of the special
FormData object. For this we will use
express-formidable is another Express middleware. It will extract the form data from the request and make it available to you when you do
This time though,
express-formidable is not built-in, we need to explicitly install it.
Go to your terminal and install express-formidable:
npm install express-formidable --save
Next, you need to
var formidable. Require it somewhere near the top of
server.js, near the other
var formidable = require('express-formidable');
Now somewhere between your
requires and your
/create-post endpoint, add this:
// require stuff aboveapp.use(formidable());// endpoint stuff below
Finally, inside your /create-post function, add this:
Refresh your server and have another go at writing a blog post.
You should now see an object in the console. The key should be
blogpost, just like the name attribute in the form on the HTML page. The value of
blogpost will be your message!