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
7 Replies
@Deno AI Helper
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.
Can you make a fresh route detect, if it is being requested by
deno run
like deno run https://localhost:3000/api/test
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:
And then you would run this script using:
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:
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 =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.
You might be able to solve this like fresh does this for
/routes/update
by simply checking req.headers.get("accept")
: