In this video Victor Mejia talks about how to set up a GET route in Express. Learn about HTTP verbs, and how to use Express to set up your API.
- [Instructor] We will now be writing our first API route which will accept GET requests to a specified path. The end goal will be to provide routes to perform CRUD operations on our data, Create, Read, Update, and Delete. When designing your APIs, you want to leverage a set of request methods that HTTP defines. Some of the most common are GET, POST, PUT, and DELETE. GET requests should be used to read data from your API.
POST requests should be used to create new data. PUT requests should be used for updating data, and DELETE requests should be used for deleting data. I recommend also reading the MDN docs to dig deeper. How would this look like for a contacts application? Say we have an API for managing contacts and the /api/contacts route. Making a GET request to this resource should return the list of contacts. When a POST request is made, a new contact should be added.
The new contact to be added should be sent in the body of this request. For dealing with a specific contact, we have this route, /api/contacts/:id, where id is the placeholder of a specific contact. Making a PUT request to this resource would update that specific contact with the updated information sent in the body of the request. A DELETE request to this resource will delete the specific contact. Now let's write some code.
Go ahead and open up index.js in your server folder. We will be using express, so let's go ahead and load that in. We create an express app by calling the express function. We will also need our MongoDB Client, we require the mongodb package and get the MongoClient object. Lastly, we need to load in our environment variables.
We do that by requiring dotenv, which reads the environment variables from the .env file, then call the config method on it. Before we start the express app, we need to connect to our database, and store the connection into a variable. Let's now connect to our MongoDB database. We do so by calling the connect method on the MongoDB driver, specifying the MongoDB uri. Remember, this is one of the environment variables.
Once a connection is made, this callback function is executed with the database connection. Let's log out a message here, specifying that we have connected to the database, and once successfully connected, we can tell our express app to start listening at port 3000. In this callback function, we assign the database connection to our variable, and a quick message telling us that our server is listening on port 3000.
We can now set up our first route. We begin by telling express to listen for HTTP GET requests at the contacts route. In the callback function, we have access to the request and the response objects. The end goal here is to read from our database and return the contacts collection. Let's first get a reference to the contactsCollection. Database is our database connection, and we can call the collection method on it, specifying the collection that we want to retrieve.
We want to retrieve every single item. To do so, we have to perform a search that we specify an empty object. This will simply return the entire collection. We convert that to an array, and in the callback function, we have access to the documents of that collection. Here, we simply return that collection and the response. We can do so by calling the JSON method on the response object.
Let's open up our terminal here using Control + ` and let's run npm run watch:server to start the API. Once started, let's first test our route using POSTMAN. We can go ahead and make a GET request at localhost: port 3000/contacts. Great, we have successfully set up our first route.
- Setting up and seeding your database
- Using HTTP requests and routing in Angular
- Creating an Angular service
- Adding JWT authentication
- Implementing a user login
- Sending JWTs to API requests