Deno official server example doesn't work

395 views Asked by At

when I go to: https://deno.land/, and I try the sample:

    import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}

on a simple index.ts, and run it on the terminal: deno index.ts it simply does not work. I get errors saying that the link on the import should not end with .ts, and the "for-await-of" can only be called when its async, and it just seems so unusual that the code sample given by the company it-self has bugs. I have tried other samples that they give but they all end up with the same error. I just want to see how to make deno servers. Any help is appreciated.

1

There are 1 answers

0
Rich N On BEST ANSWER

I think you're using the wrong command to run the code. Save it in a file as index.ts, which I think you've done, and then run it with deno run --allow-net index.ts. It should respond http://localhost:8000/ in the console. Now put localhost:8000 in the address bar of a browser, and the browser should display 'Hello World'.