Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics (2015) Doing more with Express Where do I go from here?

Anthony c
Anthony c
20,907 Points

Not clear on difference between how we used express and using express as an API

I am not totally clear on the difference between using express regularly and using express as an API.

Is the only difference that we are grabbing data from a database vs a mock folder? If that is the case, then the 'only' way to use express in the real world is an API?

If you can suggest further reading, that would be great!

4 Answers

Robert Komaromi
Robert Komaromi
11,927 Points

Technically, an API (application programming interface) is just something that describes the routes for your app (you can consider a routes.js file an API if you don't actually have the routes/protocols/interface described in some other place). The entire app, including its API, can be considered an "API app"; many people just commonly refer to it all as an API even though it's technically not correct to do so.

With that out of the way - using Express as an API (API app) means it (the API app) has a defined interface (set of routes) that a front-end web client (or another app/program) can use. Typically, JSON is the format used to transfer data between the API and the client.

For example, if you go to http://expressjs.com/ and look at the nav bar, there is a link to the Express API. This defines the interface, tools, etc for Express.

Generally, when you use Express to build an API, you expose a set of routes that will let others get data from your app as JSON instead of HTML pages. When you use Express to build a regular website, users simply visit your website and use the navigation or links they may have bookmarked or memorized to get around, and instead of JSON objects being returned, regular HTML pages are returned.

To answer your question directly, it has nothing to do with how data is grabbed for the API/app. An API app and a website can both grab data from the same database.

Further reading:

Express is used 'normally' to return HTML with hyperlinks and navigation like any other website, in the same way a regular web server would. 'As an API' is generally referring to the case where it is returning JSON, based on particular routes (URLs) and arguments being passed to it, and is used by other developers to get access to data from the service/API.

Yes, though typically a normal web server just exposes a particular directory structure from one point (the document root).

Routes are sort of made-up structures, with Node.js doing the work to determine what gets returned, based on how you define the routes.

APIs don't have to return JSON, that's just become the standard. They just have to return some representation of data that can be used for another application.

Not sure if that is any clearer...

Anthony c
Anthony c
20,907 Points

so when express is used normally, it isn't using routes (URLs) to send the html with hyperlinks and navigation?

So the main difference is (1) the data is being sent in JSON?

Or is it that (1) the data is being sent in JSON and (2) is available to other developers?

I guess I'm unclear how a normal web server is differing from an API. Doesn't a normal web server use routes (URLs)?