Search code examples
httpserverdeno

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/std@0.168.0/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);
    }
}

Solution

  • /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:

    Hope that answers your question! Have a nice day!