Welcome back. Let's finish setting up the last two routes in our server. We first have the '/profile/:userId'
which is a GET request.
That is what we want to GET the user for their homepage.
Let's do that. I'm going to add a other route.
This time it'll just be a GET request and it's going to accept '/profile/' and an 'id' – remember that if we do
it with this syntax [:id] that means we can enter in our browser anything and we'll be able to grab this 'id'
through the 'req.params' property rights.
We will again do the same thing.
Request, response
and let's add a space here to keep things clean.
And within this request, response we want to first of all grab the parameter 'id' so let's do 'const', 'id'
will be coming from 'req.params'.
And what should we do here?
Well in order to get our users we want to loop through our sample database and find the matching 'id'.
If it matches
We want to return the user.
All right.
We can avoid it any longer.
We have to do a 'for' loop here. The way we do that is we can just do 'database.users'
– although I like using 'map' for this case – we're just looping through the users and we dont really need
to create a new array – it's just a database and we want to keep that constant so we'll just say 'forEach()'
and if you remember 'forEach()' will run through the users one at a time and we'll say that if 'user.id'
which is what will get from that database equals the 'id' that we received from the 'params', if that's
the case, we will respond with JSON, that user [req.json(user)] – we'll just send the user of the matching 'id' – otherwise
We will respond with 'req.json('no such user')'.
And we can even say that its a status of 404, which means 'not found'.
All right.
Let's see if this works.
I'm going to save go back to our example. Well we know that we have '123' and '124'
and because the server restarted we won't have Ann.
You want to grab '/profile/123'.
If we click Send I get '200 OK'.
-
All right we got John.
If I do '124' I get "no such user" if we go back here
– hmm, Sally should be here.
How come we're not getting her?
And we see here that we get an error here:
'Can't set headers after they are sent.'
That is because we're looping through this and instead of returning from the loop – because once we find
the user id – we don't really need to keep looping.
So here we should just be sending a 'return res.json()'
and if the user isn't found well you don't necessarily want to do that in the loop, do we?
We want to say that for each if this user is found 'return res.json()' and we'll say that a variable
'found', which we'll default to 'false' will also get updated with 'true'
and it'll have to be a 'let' because we're reassigning it so 'found' will equal to 'true'
if we find it, and I know there's better ways of doing it.
But again I think this will illustrate a point.
We could also do a 'map' or a 'filter' to see if we actually find a person with an 'id'.
But for now let's just do 'if isn't found' [if(!found)]
So the opposite of 'found' – in that case we'll just respond with
'res.status(400).json('not found')'
Let's give that a go.
All right.
I get Sally. If I go '123' I get John – perfect and just for fun let's register
Ann again. I'm going to hit send and her 'id' is '125'.
So if I go back to '/profile/' and do '125' I get Ann.
And how cool is that?
Awesome.
So we have this endpoint working and we have one last one which is the image and this one we just wanted
to update the user to increase their
'entries' count. Every time they submit an image
we want to increase their 'entries'.
Let's do that by creating our final endpoint which is 'app.post()'
and we'll call it '/image'.
We'll get a request, response.
And again here we will have to find the 'id' of the user again.
To update their entries.
In this case we can just copy what we've done before and any time we need to copy some sort of a function
well it's a good time to actually pull it out and create your own function so you can use it in both
of these.
But for now let's just copy and paste
and instead of actually having 'params' in our case we will receive the user's 'id' from the body.
And now we want to say that – we want to copy this bottom part as well
– we'll say that if the user's 'id' matches, in that case we will respond
with the 'user.entries' and this 'entries' we want to actually increase.
So we'll say that 'user.entries++' – remember thats the short form of me doing plus one and reassigning
it.
So we'll just do '++' and now the entries of the user should increase and then we'll respond with
that. If that doesn't work, if the user wasn't found
– in that case we'll just keep it the way it is: user was 'not found'.
Let's say that and go back to Postman. We'll say '/image'.
And within this image we will have a POST request, which means a body and we can just pass the user's
"id".
In our case we'll say "Id": "123", which is Johns 'id'.
And we'll click Send.
I get "1" if I click again I get "2", "3", "4", "5" and if I go back to the 'root' which
returns our users and change that to GET I get John with five entries and Sally with zero entries.
Let's make sure it works for Sally as well.
If I do "124" I get now entries of "1" and going back to the GET users I get John with five entries
and Sally with one entry. Amazing!
We got ourselves a nice API server.
It can definitely improve.
There is a few things that we obviously want to change such as 'signin' – to not just have it for John.
We have a few repetition of code that perhaps we can extract this out into a function.
But I wanted us to have something working something that we can use so that we can connect it immediately
with our front-end.
We can finally delete our commented-out section where we planned what our API is going to look like.
Good job getting this far.
I'll see in the next video. Bye-bye