All right.
So this was a slightly more difficult challenge
and let's see if you got it right.
Now inside our compose.ejs where we've got our input and our button, in order for us to be able
to pass the value inside our input over to our server where we can log it
we have to include this inside an HTML form so let's add that now. And our input in our button needs
to go inside the form. And we have to make sure that the button is of type submit so that when you click
it it actually submits the data that's inside the form to which ever action is specified here.
Now we need to change this to a route that we're going to handle and I'm going to call it /
compose.
So we handle a post request to the /compose route in our server.
Now the other thing you have to be careful about is your input has to have a name
and this is going to be like the variable name to allow us to tap into its value.
So I'm going to call this the postTitle because that's what we're going to use this input for a little
bit later on. If you called it
anything else that's completely fine as long as you know what it's called
and you know what it refers to then it won't be a problem.
So now that we're done with our compose.
ejs, we're going to have to go into our server and handle what should happen when the page is making
a post requests to the compose route.
Currently all we have is a get method for our compose route.
So our server knows what to do when somebody makes a get request to /compose namely when
they try to view it but the server doesn't yet know what to do when somebody makes a post request to
that route.
So we have to add our app.post method targeting the /compose method and then we add our anonymous
function req and res and then we open up a set of curly braces. Inside
here is where we'll be able to console log the value of the text that was entered into the input over
here.
Now just to remind ourselves, that input had a name of postTitle.
So I need to tap into that inside this route to be able to log it.
And in order to get hold of it I need to use the request.body and this comes from our body-parser
module up here.
So the usual way that we tap into the post request data is we say request.body. the name of the
input which I happened to call postTitle.
Now if we can log the post requests that was sent to /compose and we tap into the body
using body-parser and the value of the variable could postTitle
then once we run this page and refresh it then we should be up to type in some sort of text, hit publish
and head over to our console and see it printed in here.
So how did you get on with that challenge? If it was ok then go ahead and continue to the next challenge.
If it was really difficult or you needed to see all the hints try and review the relevant parts of the
EJS module
where we went through all of this in detail.