D
Deno

help

Can you make a fresh route detect, if it is being requested by `deno run`

R!Roricchi 2.0『藤田の妻』12/7/2023
Can you make a fresh route detect, if it is being requested by deno run like deno run https://localhost:3000/api/test doing deno run https://localhost:3000/api/test on the fresh route gives me this error
>> deno run http://localhost:3000/api/test

error: The module's source code could not be parsed: Expected ';', '}' or <eof> at http://localhost:3000/api/test:1:11

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport...
>> deno run http://localhost:3000/api/test

error: The module's source code could not be parsed: Expected ';', '}' or <eof> at http://localhost:3000/api/test:1:11

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport...
@Deno AI Helper
DAHDeno AI Helper12/7/2023
Hello! It seems that you haven't included your question or the specific issue you're encountering. If you could provide more details, I'd be happy to take a look and see if there are any obvious syntax errors or mistakes. Please note that my advice is not based on the Deno documentation, and I recommend double-checking the accuracy of any advice given. If you share the code or question you have, I'll do my best to assist you with the information you provide.
R!Roricchi 2.0『藤田の妻』12/7/2023
Can you make a fresh route detect, if it is being requested by deno run like deno run https://localhost:3000/api/test
DAHDeno AI Helper12/7/2023
In Deno, when you execute a script using deno run, it's not typically used to make HTTP requests like you would with a tool like curl or fetch. Instead, deno run is used to execute a Deno script. If you want to make an HTTP request to a server from a Deno script, you would use the fetch API within that script. For example, if you have a Deno server running and you want to make a request to it from a Deno script, you would do something like this:
// request.js
fetch("https://localhost:3000/api/test")
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
// request.js
fetch("https://localhost:3000/api/test")
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
And then you would run this script using:
deno run --allow-net request.js
deno run --allow-net request.js
If you're looking to detect whether a request to a route in your Deno server is coming from a Deno script, you could potentially look for specific headers that might be set by the Deno fetch API. However, there is no guaranteed way to detect this as HTTP requests do not inherently carry information about the client software that made the request unless custom headers are set. If you control the Deno script making the request, you could add a custom header to your request to indicate that it's coming from a Deno script:
// request.js
fetch("https://localhost:3000/api/test", {
headers: {
"X-Requested-With": "Deno"
}
})
// ... rest of your code
// request.js
fetch("https://localhost:3000/api/test", {
headers: {
"X-Requested-With": "Deno"
}
})
// ... rest of your code
Then, on your server, you could check for this header: ``javascript // server.js const server = Deno.listen({ port: 3000 }); console.log(HTTP webserver running. Access it at: http://localhost:3000/`); for await (const conn of server) { (async () => { const httpConn = Deno.serveHttp(conn); for await (const requestEvent of httpConn) { const request =
DAHDeno AI Helper12/7/2023
requestEvent.request; const headers = request.headers; if (headers.get("X-Requested-With") === "Deno") { console.log("Request made by Deno"); } // ... handle the request } })(); } ``` Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. It's also important to remember that headers can be spoofed, so this method should not be relied upon for security-critical checks.
Mmrk12/7/2023
You might be able to solve this like fresh does this for /routes/update by simply checking req.headers.get("accept"):
import { Handlers, PageProps } from "$fresh/server.ts";
import VERSIONS from "../../versions.json" assert { type: "json" };

export const handler: Handlers = {
GET(req) {
const accept = req.headers.get("accept");
let path = "/docs/concepts/updating";
if (accept && !accept.includes("text/html")) {
path = `https://deno.land/x/fresh@${VERSIONS[0]}/update.ts`;
}
return new Response(`Redirecting to ${path}`, {
headers: { "Location": path },
status: 307,
});
},
};
import { Handlers, PageProps } from "$fresh/server.ts";
import VERSIONS from "../../versions.json" assert { type: "json" };

export const handler: Handlers = {
GET(req) {
const accept = req.headers.get("accept");
let path = "/docs/concepts/updating";
if (accept && !accept.includes("text/html")) {
path = `https://deno.land/x/fresh@${VERSIONS[0]}/update.ts`;
}
return new Response(`Redirecting to ${path}`, {
headers: { "Location": path },
status: 307,
});
},
};

Looking for more? Join the community!

Recommended Posts
Deno dependency top level await work aroundI have the following error while building with dnt ``` [dnt] Top level await cannot be used when disBundling a CLI written in JS with DenoHi team, Thanks for writing this [excellent blog-post (and video)](https://deno.com/blog/roll-your-Leaking operation op_http_close in testsI tried to update my repositories from Deno 1.38.1 to 1.38.4. After the update my test keeps failingDoes BroadcastChannel preserve the message order if the receiver is in a different geo-location?Does BroadcastChannel preserve the message order if the receiver is in a different geo-location?NPM CCXT Not WorkingI'm trying to run the NPM Package CCXT and I'm getting an `"ERR_MODULE_NOT_FOUND"` error any help How do you use a private docker hub image on AWS Lightsail?I'm following the guide here https://docs.deno.com/runtime/manual/advanced/deploying_deno/aws_lightsIs there a way to reset/flush my deno vk queues on deploy?Nothing seems to be queuing. I would like to reset it if possible?Return lazy struct fields from OpMy goal is to build a plugin system which allows invoking user defined code. One of the features I aRelative path in WebSocketWhy does relative path in `WebSocket` not work?Relative path in `npm:` specifierI have this in my import map: ``` "@heroicons/react/": "npm:@heroicons/react@2.0.18/" ``` And I try Transitive dependencies with node_modulesHi all, today I stumbled upon this not-sure-if-a-bug: ```ts import { LRParser } from "npm:@lezer/lrUpgraded to Fresh 1.60 but TailwindCSS not renderingI updated my codebase to Fresh 1.60 using TailwindCSS instead of twind. When I pushed the code to DeOptimize for Prod simple static vanilla JS script - WebServer App with Oak + DEJSUsing DEJS to render views. Those views are served by Oak, according to the defined routes. What is ReadableStream passed to an island component argument becomes an empty objectI am passing `ReadableStream` to an island component argument becomes an empty objectdeno.jsonc "unstable" format?I added this entry to my `deno.jsonc`: ```json "unstable": [ "broadcast-channel", "ffi", "fs"Signal value not updating from inside of `for await of` loop inside of an IIFE```ts // routes/connect.tsx import type { FunctionComponent } from 'preact' import type { PageProps