D
Deno

help

Connection refused when creating a local API server

OOoker1/2/2024
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...
Mmarvinh.1/2/2024
I'm unable to reproduce that error on my end. I get the expected response
OOoker1/2/2024
sorry for posting in multiple place do you know how to debug this?
Mmarvinh.1/2/2024
Others might have more knowledge in this area than me. It sounds like a network configuration issue on the machine you're using.
OOoker1/2/2024
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

Looking for more? Join the community!

Recommended Posts
deno deploy queuesis there a special flag or setting required for queues in deno deploy , i ceated a very simple exampWhat is the difference between importing "preact" via esm.sh or npm:...?What is the difference between importing "preact" via esm.sh or npm:...? "preact": "https://esm.sh/pDeployctl, error: Uncaught (in promise) NotFound: Failed to spawn 'start': program not found```powershell > deployctl logs a i Provisioning a new access token... i Authorization URL: https://dis there a way to make the call stack in deno larger?I already think this is a no from what I have gathered but I thought Id askwhere would i go to report a minor bug in module search on deno.land ?hi, i noticed a minor bug in module search on deno.land. it's easily replicable. where would i rep@headlessui/react + FreshI'm trying to use preact/compat to leverage @headlessui/react, but running into In the `"imports"`Is there any good tools to covert npm source code to deno source package?I want to covert some old npm source package to deno source codeEnvironment Variable always returns undefinedI'm just trying to import the values from dotenv to the code, but it always returns undefined. denoUpdated examples for "Roll your own JavaScript runtime" blog posts?Hi all. I'd like to embed Deno in a project and I discovered the excellent series of "Roll your ownExecuting module multiple timesI have a script that can be imported using `await import()` statement. what goes inside brackets is Queues in production not workinghere's the main queue code , works locally ```ts import { getOneRepoPackageJson } from "./getOneRepoRemote KV access error : Missing DENO_KV_ACCESS_TOKEN environment variablei wen through the recomnded steps of adding a .env to the root of the project and adding ```sh DENODeno.serve() with SSL cert timeoutI have a very simple script that serves a `/ping` route. After some time (randomly) it stalls and noList kv items using a partial keyHey guys am trying to figure out kv and how to get stuffout of it how do i get iteme that have the