分享关于Deno 自带的fetch的问题.
发布于 1 个月前270
我这边在本地起了简单的http服务之后尝试了一下发送请求到localhost
let res = await fetch('http://localhost:8000')
console.info(res)
结果都是:
Response {
_bodySource: ReadableStream { locked: false },
_stream: null,
url: "http://localhost:8000/",
statusText: "Gateway Timeout",
status: 504,
headers: Headers { ..., connection: close },
redirected: false,
type: "default"
}
当前已经试过: 1 python
python -m SimpleHTTPServer
2 java 手动建立tcp socket
3 koa
4 Deno 的示例
import { serve } from "https://deno.land/std@0.62.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
req.respond({ body: "Hello World\n" });
}
全都是一样的效果. 想问一下大家有相同的情况吗. 还是我这边自己的问题.
ps:
- fetch公网的url基本上都是ok的
- 浏览器访问自己起的服务也是ok的 就有点迷
共有 2 条回复
建议看MDN上关于fetch的文档:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch 你的问题只是直接把response对象打印出来了,只需要再对response对象使用text()方法即可得到响应体
let res = await fetch('http://localhost:8000') let body = awati res.text() // body就是你要的响应体
const data = await fetch(url) const resp = await data.json()
登录后发表评论!
通过Github登录