Learn Express: Routes
Build an Application Programming Interface (API) in JavaScript using the popular Express framework.
StartLearn Express Routes
Lesson 1 of 3
- 1A huge portion of the Internet’s data travels over HTTP/HTTPS through request-response cycles between clients and servers . Every time that you use a website, your browser sends requests to one or…
- 2Express is a Node module, so in order to use it, we will need to import it into our program file. To create a server, the imported express function must be invoked. const express = require(‘expres…
- 3Once the Express server is listening, it can respond to any and all requests. But how does it know what to do with these requests? To tell our server how to deal with any given request, we register…
- 4HTTP follows a one request-one response cycle. Each client expects exactly one response per request, and each server should only send a single response back to the client per request. The client is…
- 5Express tries to match requests by route, meaning that if we send a request to : /api-endpoint, the Express server will search through any registered routes in order and try to match /api-endpoint…
- 6Routes become much more powerful when they can be used dynamically. Express servers provide this functionality with named route parameters. Parameters are route path segments that begin with : in…
- 7Express allows us to set the status code on responses before they are sent. Response codes provide information to clients about how their requests were handled. Until now, we have been allowing t…
- 8Parameters are extremely helpful in making server routes dynamic and able to respond to different inputs. Route parameters will match anything in their specific part of the path, so a route matchin…
- 9HTTP Protocol defines a number of different method verbs with many use cases. So far, we have been using the GET request which is probably the most common of all. Every time your browser loads an…
- 10You may have noticed in the previous exercise that our PUT route had no information about how to update the specified expression, just the id of which expression to update. It turns out that there …
- 11Express matches routes using both path and HTTP method verb. In the diagram to the right, we see a request with a PUT verb and /expressions (remember that the query is not part of the route path). …
- 12POST is the HTTP method verb used for creating new resources. Because POST routes create new data, their paths do not end with a route parameter, but instead end with the type of resource to be cre…
- 13DELETE is the HTTP method verb used to delete resources. Because DELETE routes delete currently existing data, their paths should usually end with a route parameter to indicate which resource to de…
- 14Congratulations, you have made our glorious Express Yourself Machine fully operational! Not only that, you now have all the tools you need to create a basic CRUD API! Time to practice your skills…
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory