Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Deno's listen vs deno.land's http/server

I want to know what's the difference between the two :

This works as a 3rd party library like express ?

import { serve } from "https://deno.land/[email protected]/http/server.ts";
serve(handler);
async function handler(req: Request): Promise<Response>
{
    const url = new URL(req.url);
    console.log("Path:", url.pathname);
}

Is this a natively built-in thing in Deno ?

const server = Deno.listen({ port: 8000 });
for await (const conn of server)
{
    serveHttp(conn);
}
async function serveHttp(conn: Deno.Conn)
{
    const httpConn = Deno.serveHttp(conn);
    for await (const requestEvent of httpConn)
    {
        const url = new URL(requestEvent.request.url);
        console.log(url.pathname);
        console.log("Path:", url.pathname);
    }
}
like image 777
anjanesh Avatar asked Jun 03 '26 05:06

anjanesh


1 Answers

/std/http/server.ts is an abstraction over Deno.listen. The abstraction is designed to simplifying make the most basic http server. Anything in the Deno namespace (that is to say Deno.*) is built into Deno itself, not requiring any imports.

This works as a 3rd party library like express ?

For most practical use, you do not want to be using http/server or Deno.*. They don't do routing or middleware for you, so you should stick to more established routing frameworks.

I personally recommend one of the two:

  • (#1) Oak - https://github.com/oakserver/oak
  • (#2) Rutt - https://github.com/denosaurs/rutt

Hope that answers your question! Have a nice day!

like image 172
Lino Le Van Avatar answered Jun 06 '26 05:06

Lino Le Van



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!