Basic Steps to Building a Node Server

Recently I was asked to work as a Node engineer on a project. Of course, my imposter syndrome kicked in and told me that I was gonna biff real hard. “Nonsense” I told myself and I got right to work reviewing how to build with Express.

Turns out were gonna be using HapiJS instead. I gulped then did what I was taught and began to read the docs. Turns out Hapi is well, easy to use. But then it got me thinking about what approach should a Node engineer use when building with a new framework? What are the standard practices we can use when creating a structure for an API? Here’s what I came up with:

  1. Build a simple test server
  2. Build a route
  3. Build a method that does something with the data that is passed
  4. Test if you can pass the data from end-to-end

Test Server

Building a simple Hapi server is beyond easy. Of course we know (at least I assume ) to
npm install hapi, so building a server is as simple as

  1. Require it
  2. Instantiate a new server with a connection to a local port
  3. Start the server

Screen Shot 2016-04-13 at 9.26.59 PM

Routing

Good Node engineers know that separating server logic from routing is key to making one’s codebase more modular and easier to maintain. Hapi allows us to write this line:

Screen Shot 2016-04-14 at 10.51.16 AM

By writing the require statement in the server.route declaration, we can easily import our routes file to our server.

This brings us to our logic how to route a call to our Hapi server. We can break this process down even further into a few steps.

  1. Import your methods file
  2. Define the pathways that will call those methods
  3. Export it as a module

It will end up looking something like this

Screen Shot 2016-04-14 at 10.54.49 AM

Hapi utilizes an object to define the logic for a route. path defines the URL that handles the request, method tells the server what kind of request to make, and handler allows us to call a method to execute on the request.

Methods (or getting our server to do something when called)

The focus of building a server is get it to do something when the client requests something, right? For this example, I want my server to respond with “Why hello there” when it a GET request is requested from the /test url.

A good set of steps to follow would be

  1. Determine what dependencies are needed to handle the request and send a response
    1. In essence, what are our require statement
  2. Determine your inheritance pattern
    1. In my example, I declared a variable with an object literal named var handlers = {} which is a commonly used pattern of exported a single factory function.
      1. Check out this post about improving the module factory pattern
  3. Write your method logic to get the server to handle the request and send a reply
  4. Export the module

Screen Shot 2016-04-14 at 11.04.28 AM

By declaring an object literal and referencing it before our method name, we are able to export it to our routes file.

In Summary

Building a node server in either Hapi or Express doesn’t need to be complicated. I like to remember a saying I learned while in school, K.I.S.S (Keep It Simple, Silly). When engineering a node server, just remember the following steps:

  1. Instantiate a server using the keywords defined for the framework you are using (either Hapi or Express. Usually its server = new Hapi.Server or var app = express())
  2. Connect to local port
  3. Import the api routes in your server code
  4. Start the server

Happy Coding!

Update (5/22/2016)

Rather than rebuilding the same server.js file, I made a repo that includes boiler plate code for the Hapi library. Come check out the repo

 

 

 

One thought on “Basic Steps to Building a Node Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s