Ooker
Ooker12mo ago

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 });
}
});
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/
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
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/
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" }
{ "ok": true, "versionstamp": "00000000000000060000" }
Do you know what goes wrong? (Also asked on Stack OVerflow)
Simple API server | Deno Docs
Deno is great for creating simple, light-weight API servers. Learn how to create
Stack Overflow
curl (7): Failed to connect to localhost port 8000: Connection refused
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
Stack Overflow
Connection refused when creating a local API server
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...
4 Replies
marvinh.
marvinh.12mo ago
I'm unable to reproduce that error on my end. I get the expected response
Ooker
OokerOP12mo ago
sorry for posting in multiple place do you know how to debug this?
marvinh.
marvinh.12mo ago
Others might have more knowledge in this area than me. It sounds like a network configuration issue on the machine you're using.
Ooker
OokerOP12mo ago
hmm, I check again, and this seems that I accidentally used an WSL shell, while launched the server on a normal shell using the normal one returns the data fine