deno | response happens two times

Viewed 105

I am new to deno and created a simple code that response to my request. I put a counter over there to see how many times the response is repeating and I get it twice every time.

What is wrong with my code that makes server response twice for each request ?

import { serve } from "https://deno.land/std@0.53.0/http/server.ts";
  const s = serve({ port: 8891 });
  var counter=0;
  console.log("http://localhost:8891/");
  for await (const req of s) {
        req.respond({ body: "<h1>" + counter + "</h1>" });
                                counter ++;
  }

Response for counter on web browser : 2,4,6,...

1 Answers

If you execute that on the browser, the browser issues an additional request to /favicon, the reason why you think the server responds twice.

If you run that with a HTTP client such as curl you'll get the expected behaviour.

You may want to check the URL and respond one thing or the other depending whether it's / or /favicon.

Related