Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Microblog 1
  • Overview
  • Transcript

3.6 Creating the Chirps API

The final piece of the server side puzzle is our Chirps API, so let's go ahead and create a chirps.js file in the root of our project directory. We'll start by creating a express router just as we did in login.js. And so let's do require('express') dot and we'll call that capital R, Router function. And we can actually do two jobs at once in this line, because we know that the only thing we need to return from the Chirps API module is this router object. So I can just say, var router = module.exports = require('express').Router(), new Router object. So this way, we're creating a router object and we're basically giving it two variables that point to the same object. One is our router variable, which we'll use in here and the other is module.exports, which allows us to use it outside of this module. Now, we're also going to need our database so let's say, var db and let's create our new version of our database. So I'll say, new and then in parentheses, I'll just require ('locallydb') and then I can call the function that returns there with the new keyword. And I'm going to pass it ./.data, which is the same directory that we had earlier. So we're gonna use our same database folder. And then I'll say, var chirps this time, equals db.collection and I'll pass it the stream 'chirps'. So, it names the collection chirps. Let's see, one more thing we're gonna need here and I'll put it up at the top here is the login required function. So let me just require our login module, as well. So with all this in place, let's actually create our API and this is actually gonna be a very simple API compared to some of the others you may have seen me make in previous courses. In those courses, we may have made a full API that had methods for the get, post, put and delete functions. However, with chirps, it's going to be very, very simple. Mainly, because we can't update them, right? They can never change and we can't delete them at least for our version. So we only need to be able to get them all and to create new ones. So this is gonna be very, very simple. So we'll have our router object here and we'll call the route function and the route function is a bit of an alternative way of writing routes in express. We pass the actual route string to the route function and then we can call, get and post off of the route object returned from the route function and we don't have to duplicate the route string. So this is going to be /api/chirps and the first thing we're gonna do is call the all function. And the all function will be called for all HTTP methods and this is where we're going to use login.required. So to view any part of our API, we want the user to be logged in. However, if they are logged in, then we can have our get request here and this is what we will use just to return all of the chirps. So what I can do is res.json and we can say, chirps, which is our whole collection .toArray and that will convert our whole collection of chirps to an array and send them all back to the user and so that is the first method we need. And actually, that's half of our API, that's getting all of the chirps. The only other thing we need to be able to do is post a new chirp. So let's create our post handler here, where we have our callback function. And in here, the first thing we want to do is create our chirp. So let's say, we have our new chirp coming in. It's the req.body for starters, that's really only gonna have the text of the chirp. Now there are a couple of other properties that we want to add to this chirp. First of all, we want to add chirp.userId. So we can know who made this chirp and to do this, we will do a req.user, which passport gives us. This is the currently logged in user and then we can do .getcid to get their ID property and so we can mark this as their own chirp. Now eventually, this is all we will need to store on a chirp. However, when we first start writing our chirp display UI, it will be easier if these chirps have a few extra properties. So I'm just gonna add a comment here TO BE REMOVED, so that I know later we're gonna remove these properties. And let's add chirp.username = req.user.username and we're gonna add chirp.fullname = req.user.fullname. And then finally, chirp.email = req.user.email. Now I don't really like having to store these properties on the chirps, but this will make it a little bit easier to write some of our front-end initially. And then eventually, we'll be able to actually look up the user just based on this ID and then we'll be able to remove these lines. However, for now, it will be easier to have them in place. So that's what we'll do and then let's do chirps, which is our collection.insert and we will insert that new chirp and we will get that id. And then finally, we can do res,json and we can send back chirps.get and we will get the chirp that we just put in by passing that id and there you go. That is our entire API. Now we don't have to remember to export this at the bottom, because we're doing it right up here with module.exports. And now back here in server.js, it's as easy as adding a .use statement. I'll add it under login.routes and in here, we can just (require('./chirps') and there we have it. That means our Chirps API is ready to go, so we'll be able to see this API in action as we build the frontend of our application, which we will start in the next lesson.

Back to the top