- [Instructor] In this video we'll take the new message
we just received in our app.post endpoint,
and add it to our messages array.
So the first thing we'll need to do,
is take out messages array out of the app.get endpoint.
And we'll paste it outside of that
so that both endpoints have access to it.
Now since we're going to be modifying it,
we will need to change it from a constant to a var.
Then in our app.post endpoint below our console log
we'll modify messages.
So we'll use messages.push,
and instead of passing in request.body,
I'll create a let msg for message,
and I'll set that to request.body.
Then I'll update line 19 to console log our new message.
Then I'll push the new message,
and finally I'll also respond with that new message
instead of just an empty object.
to respond with the object you just added or processed.
So now the final piece
is to verify that the message was added
to the messages array.
So I'll add in one final console log
which will be temporary,
and it will display our messages array.
I'll save that, then back in Insomnia,
I'll go ahead and hit send again.
We can see our new message is sent back to us,
and in our terminal we can see that the new message object
is displayed from line 19,
and then our entire array on line 22 is displayed,
but there is a slight problem.
Our original messages are just raw strings
inside the array, but our new message
is adjacent object within message property.
To fix that, we can simply push msg.message on line 20.
Let's save that and give it another try.
I'll hit send,
and now we can see that we have just the raw string
for our fourth message.
And if we go back to our front end and refresh,
we can see that new message is being displayed.
So we saw how to save our new message
to our message array,
and how to send a message using third party applications
such as Insomnia.
Next we'll look at how to create the new component
on our front end that will allow us to send new messages.