You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Deno中PHP的$_SERVER["REQUEST_URI"]等价变量是什么?

PHP的$_SERVER["REQUEST_URI"]在Deno中的等价实现

在Deno中没有直接对应PHP $_SERVER["REQUEST_URI"]的全局变量,需通过请求对象的属性结合URL类解析获取,核心是利用Request对象的url属性来提取完整请求路径(含查询参数)。

原生Deno HTTP服务实现

使用Deno标准库http/server模块时,遍历请求迭代器拿到的request对象自带url属性,将其传入URL类即可解析出对应内容:

import { serve } from "https://deno.land/std/http/server.ts";

const server = serve({ port: 8000 });

for await (const request of server) {
  const urlObj = new URL(request.url);
  // 拼接pathname和search,得到与$_SERVER["REQUEST_URI"]完全等价的内容
  const requestUri = urlObj.pathname + urlObj.search;
  console.log(requestUri);
  request.respond({ body: `当前请求URI:${requestUri}` });
}

常用框架(如Oak)实现

如果使用Oak这类主流Web框架,可通过上下文对象的request.url获取同样的URL对象:

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
  const urlObj = ctx.request.url;
  const requestUri = urlObj.pathname + urlObj.search;
  ctx.response.body = `当前请求URI:${requestUri}`;
});

await app.listen({ port: 8000 });

关于你遇到的"not a function"错误

大概率是误将request.url(属性)当成函数调用,比如写了request.url()而非request.url。url是Request对象的只读属性,本身不是方法,直接访问即可,无需加括号调用。

内容的提问来源于stack exchange,提问作者user18186629

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 21:52:43