- In this video, we'll look at how to post data
to the back-end.
To start with, we'll need to create a new endpoint
that takes in data instead of serves it.
But once we have that endpoint, in order to test it out,
we'll need to use a third party application,
such as Insomnia or Postman.
But I'll be using Insomnia,
since it's pretty straightforward to use.
Although, the installation process for me
was a bit glitchy.
Though, if you'd like, you can use Postman,
and just follow along with the same setup.
So, go ahead, and select Download
if you're going to use Insomnia.
And, in my case, I downloaded Windows 7+.
And then install it.
Now, before we launch it and test our endpoint,
we'll need to create our endpoint.
So let's go into our back-end project.
And below our app.get function,
we'll create a new one called app.post.
And since this is a post action instead of a get action,
we can actually use the same location or URL.
So, I'll use /messages.
And, again, I'll pass in an inline function
with request and response.
And, to begin with, I'd simply like to console log
the data that's sent to our app.post endpoint.
So, I'll use a console.log.
And I'll use request.body to display the message body.
And let's save that.
Let's go ahead and start Insomnia or Postman.
And once Insomnia has started, you can go ahead
and create the new Request.
I'll just leave the default, but select POST instead of GET.
And, for body, we want to select JSON.
Then I'll hit Create.
And then we'll need to supply the URL.
So I'll copy it from line 23, and then I'll paste it.
Now, for our body, let's create a JSON object
with a property called message.
And then I'll wrap the property name in double quotes, also,
so that it's valid JSON.
Now, let's send that.
Now, let's check our back-end project.
And we can see we're getting an undefined result
in our console log.
So, we're getting something.
The endpoint is triggering.
But we're not able to see what it is.
Our Insomnia request is hanging.
So, I'll cancel this request.
Let's figure out first why it's hanging.
the body that we're sending,
but then we're not doing anything afterwards.
So, let's simply call response dot JSON,
and we'll just send in an empty object for now.
Save that, and try sending it again.
And we can see it's no longer hanging,
and we're getting our empty object.
So, now, let's see why we're not able
to see that test message.
The problem is express does not come
So we need to install that.
So let's go back to our second terminal window,
and we'll use NPM
install body-parser.
Now we need to set up the middleware
just like we did with our core's middleware.
So, below line two, we'll add a new constant
called bodyParser.
We'll set it to require body-parser.
Now we'll copy the name of our constant.
And, below our first app.use on line seven,
we'll add another app.use,
and we'll pass in bodyParser, and we'll specify
we're expecting JSON.
And I'll save that.
Now, let's go back to our first terminal window.
And let's go to Insomnia
and try sending one more time.
And we can see we are now able to see our JSON object
from our request dot body call on line 18.
So, we've created an endpoint
that allows us to take in a new message.
Next, let's take a look at how to update
our messages array with that new message.