RESTful API Using Node and Express 4
⟹Our application
➞We are going to build an API that will:
➞Handle CRUD for an item (we're going to use bears)
➞Have a standard URL (
http://example.com/api/bears
and http://example.com/api/bears/:bear_id
)➞Use the proper HTTP verbs to make it RESTful (
GET
, POST
, PUT
, and DELETE
)Return JSON data
➞Log all requests to the console
➞All of this is pretty standard for RESTful API.Feel free to switch out bears for anything that you will want to build for your application (users, superheroes, beers, etc).
⟹Getting started
➞Let's look at all the files we will need to create our API.➞We will need to define our Node packages, start our server using Express, define our model, declare our routes using Express, and last but not least, test our API.
⟹Defining our Node Packages
➞As with all of our Node projects, we will define the packages we need in
package.json
. Go ahead and create that file with these packages. ➞What do these packages do?
express
is the Node framework. mongoose
is the ORM we will use to communicate with our MongoDB database. body-parser
will let us pull POST content from our HTTP request so that we can do things like create a bear.
⟹Installing Our Node Packages
➞npm will now pull in all the packages defined into a
node_modules
folder in our project.➞
npm
is Node's package manager that will bring in all the packages we defined in package.json
. Simple and easy. Now that we have our packages, let's go ahead and use them when we set up our API.➞We'll be looking to our
server.js
file to setup our app since that's the main
file we declared in package.json
.⟹Setting Up Our Server
Node will look here when starting the application so that it will know how we want to configure our application and API.➞Base Setup In our base setup, we pull in all the packages we pulled in using npm. We'll grab express, define our app, get bodyParser and configure our app to use it. We can also set the port for our application.
➞Routes for Our API This section will hold all of our routes. The structure for using the Express Router let's us pull in an instance of the router. We can then define routes and then apply those routes to a root URL (in this case, API).
➞Start our Server We'll have our express app listen to the port we defined earlier. Then our application will be live and we can test it!
⟹Starting our server and test
➞Let's make sure that everything is working up to this point.
➞We will start our Node app and then send a request to the one route we defined to make sure we get a response.
⟹Database and bear model
➞We'll keep this short and sweet so that we can get to the fun part of building the API routes.➞All we need to do is create a MongoDB database and have our application connect to it.
➞We will also need to create a bear mongoose model so we can use mongoose to interact with our database.
⟹Creating Our Database and Connecting
➞We will be using a database provided by Modulus.
➞You can definitely create your own database and use it locally or use the awesome Mongolab.All you really need is a URI like below so that your application can connect.
➞Once you have your database created and have the URI to connect to, let's add it to our application. In
server.js
in the Base Setup section, let's add these two lines.➞That will grab the mongoose package and connect to our remote database hosted by Modulus. Now that we are connected to our database, let's create a mongoose model to handle our bears.
No comments:
Post a Comment