Creating Routing/Controller in Deno Server(From Scratch)
#
IntroductionDeno provides a standard package std/http
for working with http/https
server. However, the routing request to different Control is not supported out-of-box. Demo model is same as NodeJs. Saying that, Deno insist you to use module like oak
. I will also recommend you to use this module.
Here in this tutorial, I will explain how you can build Super cool Router from scratch.
π‘Learn More:
Before creating a routing, lets create a basic server.
Run this deno run examples/basic_server.ts
Open browser at http://localhost:8080/. You will see hello world
.
π₯Fact:
If you have not read my hello-world Article. I will recommend you to please read it.
Breakdown:
Here, whenever you request anything to server. It will return you hello world
in response. Adding route will be done inside for-each
loop. Let's add first route.
Run this deno run examples/basic_server.ts
Open browser at http://localhost:8080/. You will see Hello Mr. Unknown
. If you try some other URL, you will see 404! Page Not Found!
.
Breakdown:
- Get the current request URL using
req.url
- Switch between url
/users
and respond accordingly.
We can do something like this. The only issue with this approach. We can't have dynamic route like /users/1234
where is 1234
is the id of user.
As solution, Instead of directly matching one to one. We can use regex
to match URL
and get the id
of user.
Run this deno run examples/basic_server.ts
Open browser at http://localhost:8080/. You will see {"name":"Sam"}
. If you try URL with id 5
, you will see USER NOT FOUND
.
Breakdown:
Using regex match we achieve what we had needed. However, writing regex of complex pattern could be an issue. Let's use our first library as file. We will use path-to-regexp
from pillarjs
. This is the same library used by express server
in nodejs.
Re-run app again. You will see no difference. Nice!
Here adding too much business logic in same for-each
loop can leads to many issue. The major concern is maintenance. So let's move to controller/handler
.
If you run app and request app with same input as previous. You will see same output. We just move the User logic to separate handleUsers
function.
Nice! All good. However, managing these many route path and regex is tough task and hard to maintain as well.
As solution we can create a list/array of routes. The interface for Route
could be
Let's create two handler. One for users, another one for posts.
Create a handler for Page Not Found
.
To match URL pattern
, We can loop over all the routes and call the respective handler.
The complete code will be like
Don't worry, We will further break down the entire code and do required clean up.
Breakdown:
- In above sample, The
router
function will be called on each request. - This router function will loop on each
Route
fromroutes
and try to match. - Once match found, it will call respective handler.
Code:
Code can be found at examples/basic_server.ts
Let's give final touch and break into files.
Create a controllers.ts file
Bonus:
I have added static page handler[staticFile]
for static assets.
Move all router logic in router.ts file
Finally the main server with request logger: final_server.ts
Run this deno run examples/final_server.ts
Open browser at http://localhost:8080/static/home.html. You will see Magic
.
Good Job! Thanks for support in advance. Please do follow me, subscribing and clapping on https://deepak-v.medium.com/
#
All working examples can be found in my Githubhttps://github.com/deepakshrma/deno-by-example/tree/master/examples