177177177
17717717712mo ago

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

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...
7 Replies
177177177
177177177OP12mo ago
@Deno AI Helper
Deno AI Helper
Deno AI Helper12mo ago
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.
177177177
177177177OP12mo ago
Can you make a fresh route detect, if it is being requested by deno run like deno run https://localhost:3000/api/test
Deno AI Helper
Deno AI Helper12mo ago
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 =
Deno AI Helper
Deno AI Helper12mo ago
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.
mrk
mrk12mo ago
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,
});
},
};