Node.js Native Router
It seems that there is a never ending battle when it comes to web frameworks & libraries with regards to benchmarks, heck, I’ve even seen people comparing different languages time & time again. But since I enjoy working with JavaScript, I thought it might be neat if we just removed any external dependency in terms of causing a performance bottle neck. Why not create our own performance issues? It just seems too easy to point the finger at a given framework or library & state that the performance issues arises there. Why not take more ownership I say?
So, to start our simple Node.js router library, we’ll first need an application class of some kind, if we were to look at the Express library, you could think of it as the express function itself, where 99% of people assign a variable called app to the returned value.
I think we’re off to a good start, it’s simple, but we know out of the gate, we’ll be storing information regarding routes here, what method is used, what URI we want to use to access the associated handler, etc. I’m not going to over complicate it, I’ll try to keep this as simple as possible, just to ensure that everyone can follow. Heck, there are probably much better ways of doing this than what I’m going to do here on out.
So, let’s keep this one simple, that’s a part of the goal here, simplicity in addition to the performance benefits of not using any external libraries. For now, I’ll just focus on implementing a means to allow consuming code to implement a CRUD service, by all means, it should be easy enough to add additional methods such as patch, head, options, etc, if you so desire.
Simple enough, we’re just pushing stuff to the routes array, nice, it’s simple enough for novice JavaScript developers to follow. But now let’s consider a start method, we know that we’re creating a server where it can handle a bunch of request from a bunch of different URI’s & so on. Let’s stick to the current pattern here, let’s strive for some degree of simplicity here.
Now we’ve progressed, we’ve created a server, it’s alive! But in order to make sure that we’re not using any external dependencies that don’t come shipped with the node installation, let’s stick to using dependencies such as the http dependency.
But let’s plan this next step out, we know that we’ll need to do some of the following:
- Find a route based on some pattern matching.
- Extract some path parameters.
- etc.
So with that in mind, let’s continue. Let’s consider how we’re first going to find the necessary route. 🤔 – Again, not making any performance optimisations before they’re necessary & keeping it simple, that’s the ultimate goal right now.
Okay, this is pretty neat, again, it’s simple enough, we know that we can find a given route, for a given path. But now we need to think about some more steps before we go on to process the handler, accommodate to potential errors, etc. So let’s just progress on to a somewhat more complete implementation where we’re actually ‘parsing’ parameters & attaching ’em to the request object.
And finally, we’ve reached the end, where we have an application class that allows us to implement some relatively basic HTTP request handlers. If you like, here’s some complete code that I’ve been tinkering with to verify that the code actually works. Granted, this might not be considered production ready code, I’ve not done anything fancy like implementing a means to handle middleware or anything along those lines, I’ve gone out of my way to make it as simple as possible, putting a primary focus on performance & simplicity.
Granted, I’m certain that there are people out there that are a lot more intelligent than myself & will notice some majors flaws with this approach. But remember, I mostly did this as a bit of fun, just to see how hard it is to implement something like express, but without installing any additional npm packages. I mean by all means, if you want to use this code & build on it & do something cool, please, be my guest. 🙂
But I do recommend more JavaScript developers go out of their way to learn the underlying language itself, don’t rely completely on 3rd party tools, libraries, etc. Granted, again, maybe don’t do this in a production setting, but to tinker & to experiment, why not? Who knows, you could just make the next big thing? 😅