我一直在和Deno Oak一起玩。查看了一些基本的路由示例,其中没有一个使用请求或响应的类型。
router
.post("/api/v1/products", addProduct)
const addProduct = async (
{ request, response }: {
request: any;
response: any;
},
) => {
const body = await request.body();
if (!body.value) {
response.status = 404;
response.body = {
success: false,
msg: "No data",
};
}
在上面的示例中,请求和响应属于any
类型。我试图用以下与身体不兼容的类型替换它?
import { ServerRequest, ServerResponse } from "http://deno.land/x/oak/mod.ts";
如果有人能指出我一个相关的例子或对此有所了解,我将不胜感激。
ServerRequest
和ServerResponse
是Deno net使用的类型。Oak 使用Request
&Response
const addProduct = async (
{ request, response }: {
request: Request;
response: Response;
},
)
你可以看到 OakResponse
有一个方法toServerResponse
,可以从 Oak 的 Response 转换为 Deno net ServerResponse。
/** Take this response and convert it to the response used by the Deno net
* server. Calling this will set the response to not be writable.
*
* Most users will have no need to call this method. */
async toServerResponse(): Promise<ServerResponse> {