It's been awhile since I played around with Node so I decided I would setup a very simple web api with a few different routes. In my searching it seemed everybody starts off with a basic hello world using just node then move on to using a middleware to configure all the routes. Using something like Express can make a lot of sense. The code is certainly cleaner and easier to read, but like everything in software development there are trade offs.
One trade off is the size of your project. As of 4.16.4 express has 30 dependencies alone and some of those also have their own dependencies. If you look up a tree view of all the nested dependencies Express has a total of 49 nodes and 82 links. Other middleware options aren't much better. Restify has 84 nodes and 113 links and PerfectAPI has 129 nodes and 187 links. There are numerous other middleware options available they will all have dependencies in addition to plain node. If you are a person that doesn't want to pull in lots of dependencies you can't control or don't understand or you don't want your node_modules folder to be too large then maybe doing it all in node is your best option. With the size of the dependencies in mind I decided to create a web api using only the modules that are built into node.
At the top I'm requiring the built http and url modules from node and I'm requiring one of the custom controllers that I created. So far this is all straight forward. Next I'm creating the HTTP server that will listen for the requests. I'm going to use a params property off of the request object to pass around any optional parameters from the route so I'm making sure the params property exists and is initialized. Next I'm parsing the incoming URL to get the path. Parsing the request url returns a hydrated URL object, see documentation here. Now we start to get into the route configuration.
As you can see I have a basic / path for handling the home route. Next is the task route. Inside task I'm checking which route to call, getting optional parameters, and determining if this is a GET request. I'm using the match method off of the javascript string object to pass a regular expression that checks if there are any digits in the path. If there is and it is a GET then add the task ID to the params and call one function in the task controller otherwise I call a different end point.
For this example I didn't put any logic in my controller but you could put anything you need in your controller.
There you have it. You can create a web api and all your different routes using only Node, no extra middleware. Is it worth it? Maybe, maybe not. In this example I don't have any extra dependencies which makes my api incredibly small and lightweight. On the other day it will become more difficult to maintain as the application grows and you have more routes with more and different optional parameters. As the application grows unit testing your routes will become even more important. Stay tuned for Part 2 where I clean up the app.js a bit and add some unit tests.
Thanks for making it this far and Happy Coding.
Sean Wernimont The Blind Squirrel Copyright 2015-2020
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|