- [Instructor] I'm back in my service registry.
And of course there's a module for pausing
and comparing semver strings.
And let's install it by running npm install --save semver.
Next I will open ServiceRegistry.js.
And on top of it I just add const semver
equals require semver.
Now I have to add this method that gets a service
given a name and a version.
I simply call get(name, version).
And I now want to filter the services
and find those that match my criteria.
So I add const candidates equals object.values.
So this will give me the individual entries
of this.services.
And on each of those entries, I call filter.
And this filter function gets the service in its callback.
And this is an ES6 shortcut syntax now
where I simply do service.name equals name.
So I'm comparing is this a service with the right name?
And if so, I compare semver.satisfies service.version
with the requested version.
As mentioned before, I also want to add some randomness
for load balancing.
So I need a function that will randomly return candidates.
And this can be done by using math.floor on math.random
and I multiply that with candidate.length.
This will now work for zero, one, or more entries.
If there is no entry, I get a false value back.
And if there is one entry, I get this one entry back.
And if there are more it will randomly
select one entry from the candidates.
Now of course, I also have to adapt or add the route
to service js.
So I go into service js
and there I look for the get find route
and there I add const, curly brackets, servicename,
serviceversion, equals request dot params.
Now I query the registry.
I do const service svc equals ServiceRegistry.get.
And I want the right servicename and serviceversion.
What should happen if there is no match,
so if there is no service found,
I will return res status.
And it's a 404 for not found.
And I will just add some json as information.
Let's call this result service not found.
Otherwise, I will return return res json
with the service that we found.
Let's remove the next variable up here.
And we should be good.
Let's start the service registry now.
Npm start.
Now let's head over to Postman to try that out.
So let's first create a few entries
with different versions and different ports.
So, one one one.
And one one two.
And one one three.
And one one four.
And let's vary the port, for one one five.
And then let's create one more with one two zero.
And port 3009.
We should now have a set of entries in our service registry.
Now let's try to query that.
So it will get request.
And I'm doing a find.
And let's first start with just requiring version one.
And if you look at the version stream
in the result, you see that we now get
different matching versions randomly.
So if I now add a one dot one,
so this matches now more exact.
And I will get just results for one dot one.
And the patch version is now a varying.
And of course, it can also just request
one very distinct version.
And this will then give me services
that match exactly that version
that's exactly what we wanted to achieve.
That was actually pretty simple, right?
But this gives our service registry
a lot of flexibility and power.