So we want to get started
by building this example API.
And actually, here's a challenge for you.
Try building this on your own,
though in a slightly simplified version,
I want you to build a node express web server
that has one route, a route to GET/quote,
where you then return one string.
Always the same string, no randomness, no list of strings,
no database that needs to be connected,
only a route that returns a string,
but, don't return it as plain text,
but instead as JSON data,
because that is the default data format for exchanging data.
This is a challenge for you,
you won't need anything you wouldn't have learned thus far,
I'll give you a couple of seconds to pause the video
and try this on your own
before we're then going to build this together.
So, did you succeed?
Let's build it together.
For this I'm in a brand new folder,
and I will actually start by opening the terminal
and running NPM init-y.
This will initialize this as a NPM managed project
where we thereafter can use NPM install,
since we now got a package.json file,
and that's a key command
so that we can then install all these packages we need
and start building our node express project.
Now once that's done,
we can start installing packages, which we need,
and in this case to get started,
it's only one package, the express package.
Since I wanna build a node express
node JS application here, which will be our web API.
And with that installed,
we can add the good old app JS file again.
And we could now also add sub folders for controllers
and routes and so on, but,
I won't do this here for this simple API,
though of course you could do it, but if you do it,
keep in mind that you won't need a views folder,
so we won't use the MVC pattern here.
You can use controllers and models,
but you don't need views,
because the core idea of a web service and web API
is that there are no views.
There will be no HTML code that we generate and send back.
So there are no views.
Nonetheless here, I'll keep it super simple
and work only in this app JS file
and in there, I'll then import express
with that good old syntax that we already know,
like this,
create my express app by calling express as a function.
And then I listen on port three thousand.
Now, I want to have one route that I wanna handle,
and that can be done with app.get.
We could also create an express router,
as we did it in most sections in this course.
But if you remember, we got started with express
in exactly the way I'm showing it to you here.
You can also set up request listeners.
So to say, directly on this app object,
we just typically use the router
so that we can outsource those route definitions
in standalone files.
But since I don't plan on doing this here,
we can register a get route like this on the app object.
And then as a first parameter value, pass our path to it,
which in this case should be slash quote.
And then, we can point at a function
or define an anonymous function,
which still gets request, response and next,
that doesn't change because express doesn't care
about what we build, so that always works in the same way.
Everything about writing this back end code
works in the same way,
there is no difference between web APIs and websites
when it comes to that.
And hence here we can now send back our response
by calling res.what.
Before we most of the time called res.render,
because we wanted to render a template.
Here this doesn't make any sense though,
because now as mentioned before, we won't render a template.
We have no templates.
And we don't want to send back HTML code,
which is what res.render would do.
Instead we can call a method on res
that we have used before in the course as well.
And that's the JSON method.
We used it before in the course for all our routes
that were triggered with Ajax requests.
And in the end what we did back there,
is we already built our own first API.
We just didn't call it like that.
But we built end points, HTTP method path combinations,
that sent back data and did certain things
when requests reached those end points.
Of course we can call the routes
that return HTML code end points as well, but this pattern,
which we see here, that we return JSON code,
is exactly the same pattern we also used before
for those Ajax requests.
Because those Ajax requests also didn't want HTML,
instead they wanted JSON data.
So here we can now pass an object to this JSON method,
and it will be converted to a string
in that JSON format automatically by express then,
and there we could set a quote key
or whatever you want to name it,
and then we could add a quote like,
"As you dive deeper into web development,
web development will dive deeper into you!"
So a very philosophical quote,
of course you can choose any text,
but this is now the quote which I'll return here,
and that is my API end point, this registered route here.
And again we could have done this
with express router as well, but that works just as well.
And we need nothing else, no other middleware,
because we for example have no static files
that need to be served since we won't render any webpages,
we won't have any static files,
because we have no CSS code and so on.
If we're just building an API,
that's the idea behind an API, that we just exchange data
and that we don't render any web pages that might need CSS.
So therefore we have no middleware that serves static files,
nothing like that,
though in the later examples of this section
we will also add some middleware.
Because there are a lot of use cases
where you need that as well.
Nonetheless that's the first simple API here
and we could now install node one,
or to keep things simple here,
just manually run this by running node app.js.
And now this web server is up and running.
And now to view its effect,
we can enter local host:3000/quote.
And this will send a GET request to this path
and hence it will trigger this API end point.
So if I hit enter here,
here's my JSON response output in the browser.
And that is our first basic web API and API end point.
And as you see, there's nothing fancy about that.
And that indeed is one of the key takeaways I want you
to take away from this lecture and this section.
APIs are just web servers and projects
as we built them throughout the course.
In the end it's just the return value,
the response value that's different.
We always exchange data here, we don't build the views,
we have no CSS code,
instead it's all about what happens on the server
and the data we get and use and the data we send back.
And with that we can now take our next steps
and make this API at least a little bit more complex here.