OokerO
Denoβ€’3y agoβ€’
6 replies
Ooker

Connection refused when creating a local API server

I'm following Simple API server | Deno Docs:
1. Create server.ts:
const kv = await Deno.openKv();

Deno.serve(async (request: Request) => {
  // Create short links
  if (request.method == "POST") {
    const body = await request.text();
    const { slug, url } = JSON.parse(body);
    const result = await kv.set(["links", slug], url);
    return new Response(JSON.stringify(result));
  }

  // Redirect short links
  const slug = request.url.split("/").pop() || "";
  const url = (await kv.get(["links", slug])).value as string;
  if (url) {
    return Response.redirect(url, 301);
  } else {
    const m = !slug ? "Please provide a slug." : `Slug "${slug}" not found`;
    return new Response(m, { status: 404 });
  }
});

2. Run deno run -A --unstable server.ts. The site says "Please provide a slug"
3. Run:
curl --header "Content-Type: application/json" --request POST --data '{"url":"https://docs.deno.com/runtime/manual","slug":"denodocs"}' http://localhost:8000/

I get this error:
curl: (7) Failed to connect to localhost port 8000 after 0 ms: Connection refused

Searching for the error, I think I need to replace localhost with 127.0.0.1:
curl --header "Content-Type: application/json"   --request POST   --data '{"url":"https://docs.deno.com/runtime/manual","slug":"denodocs"}'   http://127.0.0.1:8000/

This doesn't help either.

In my understanding, it should return
{ "ok": true, "versionstamp": "00000000000000060000" }

Do you know what goes wrong?
(Also asked on Stack OVerflow)
Deno is great for creating simple, light-weight API servers. Learn how to create
Simple API server | Deno Docs
Stack Overflow
Hello I am testing the endpoint and when I run php testing.php I get this error. Any help would be appreciated it.

Fatal error: Uncaught exception 'Guzzle\Http\Exception\CurlException' with
curl (7): Failed to connect to localhost port 8000: Connection refused
Stack Overflow
I'm following Simple API server | Deno Docs:

Create server.ts:

const kv = await Deno.openKv();

Deno.serve(async (request: Request) => {
// Create short links
if (request.method == "P...
Connection refused when creating a local API server
Was this page helpful?