skip to Main Content

I am new to Deno and I was wondering what the equivalent variable of the PHP variable $_SERVER["REQUEST_URI"] would be?

I tried countless functions which were returned as "not a function".

2

Answers


  1. Deno has a couple of different web server APIs (and then there are frameworks built on top of them). But I think most (all?) of them provide the handler with a Request object, which has a url property telling you what the requested URL was.

    Login or Signup to reply.
  2. PHP can have a global variable storing the request URL because it works in a different way than Deno.

    In Deno, you usually get the URL inside of some request handler function in one of its parameters, but you usually don’t even need that if you are using any framework to help you handling those requests (see examples below).

    Using the bare serve utility from the std library, you can handle requests like this:

    import { serve } from "https://deno.land/[email protected]/http/server.ts";
    
    function handler(req: Request): Response {
        console.log("Request URL:", req.url);
        return new Response("Response: OK");
    }
    
    serve(handler);
    

    In the above example the req.url exists because it was passed to this handler() function every time it was invoked (for every request) because this function was registered as a handler for all requests by invoking serve(handler);

    If you save it as main.ts then you can run this program with:

    deno run --allow-net main.ts
    

    The default port is 8000 so you can run curl to make a request like this:

    deno run --allow-net main.ts
    

    which will display:

    Request URL: http://localhost:8000/abc
    

    Now, this is quite low level API and in practice you would probably want to use a library like oak. E.g. to have an equivalent server using oak, which works differently than serve from thee previous example, you’d use something like this:

    import { Application } from "https://deno.land/x/oak/mod.ts";
    
    const app = new Application();
    
    app.use((ctx) => {
      console.log("Request URL:", ctx.request.url.href);
      ctx.response.body = "Response: OK";
    });
    
    await app.listen({ port: 8000 });
    

    So here, you access the URL not be req.url but with ctx.request.url.href (or starting with something else than ctx if you named your parameter differently). The .href is here to get the full URL as a string because ctx.request.url is an object – an instance of URL).

    But you usuall don’t access your URL lik that, but instead you define routes with separate handlers that get run for the specific URLs, e.g.:

    import { Application, Router } from "https://deno.land/x/oak/mod.ts";
    
    const router = new Router();
    router
      .get("/abc", (context) => {
        // we know that so we can hardcode it ...
        console.log("Request for: /abc");
        context.response.body = "Response from /abc";
      })
      .get("/def", (context) => {
        // ... or we can access the context
        console.log("Request for:", context.request.url.pathname);
        context.response.body = "Response from /def";
        
      });
    
    const app = new Application();
    app.use(router.routes());
    app.use(router.allowedMethods());
    
    await app.listen({ port: 8000 });
    

    Note that we don’t have to test the URL inside of those handlers but of course we can. They already know which URL they are serving because the oak library runs them for the correct routes that they need to serve.

    So it all depends on what libraries of framework you are using to handle the requests – which you would usually do for production code instead of using the serve directly (unless you have some very specific needs).

    To find a good framework for for your needs, see:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search