Creating a RESTful API (Express series 3)

So in the last post we went over the project structure. This time, we’ll actually modify some of the code to create our own RESTful web service.

If you don’t know what a RESTful web service is, you should check out the wikipedia entry for it.

As discussed in the previous post, all of our routes are defined in files located in the routes folder which are then referenced and collated in the app.js file. So, to create our own RESTful API, we need to do something in this area.

Lets checkout the index.js file and see whats in it.

This is the typical structure of any nodejs module. Top section is to declare dependencies and instantiate objects. Middle part is to actually do things. And the final bottom part is to clean up and export the module. This file says that when the root (/) is accessed by a HTTP GET method (indicated by router.get), call a function. This function is defined as function(req, res, next) {...}. The three variables, req, res and next are function parameters. I won’t go into the details regarding basic JavaScript in any of these tutorials. If you have any questions, check out some of many JavaScript resources available on the web.

These parameters req, res and next are the request object, the response object and the function call to the next module in the middle ware stack respectively. The line res.render('index', { title: 'Express' }); is using the render function of the response object, asking express to render the template 'index' with parameters specified by the JSON object { title: 'Express' }. Since Express knows that we are using Jade templating engine (defined in app.js), it will go to the views folder and find the file named index.jade and will render it.

Right, so now that’s out of the way, lets go and create our own service. For the purpose of this tutorial, we’ll create a health check web service. When it gets called, it will return OK status indicating that the server health is ok. Since this will require a GET request to be invoked, we’ll be using router.get and hence the first line of the code will be very similar to the already existing one.

Next, we need to change two things. One is the URL reference. Currently, its set to '/'. Because this has already been defined, lets modify it to our own path. This can be anything you want but for the sake of simplicity, lets make it /healthcheck. Your code should now look like this:

The code doesn’t do anything yet. Lets change that. Remove the comment // My health check API and put the following logic in it.

After your change, the file index.js should look like this:

Awesome. Now go back to your command line and run npm start command to start your server. Once started, go to http://localhost:3000/healthcheck on your web browser. You should see the following output:


Now this isn’t very clean as its returning plain text instead of proper JSON response. Shut down the server and go back to your IDE. Modify the code in the /healthcheck router in index.js to be the following:

Start the server and navigate back to http://localhost:3000/healthcheck in your browser. You should see a JSON response this time:

Great work! You’ve just written your first RESTful web service in Node.js using Express! In the next post we’ll take this to a next level and migrate what we’ve written to a separate file to reduce clutter and make the code a bit cleaner. Enjoy!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.