我一直在和德诺·奥克一起玩。查看了一些基本的路由示例,其中没有一个使用请求或响应的类型。
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类型。我尝试用以下类型替换它,这些类型与body不兼容?
import { ServerRequest, ServerResponse } from "http://deno.land/x/oak/mod.ts";如果有人能给我指出一个相关的例子或者给我一些启发,我将不胜感激。
发布于 2020-06-23 19:48:40
ServerRequest和ServerResponse是Deno net使用的类型。Oak使用Request和Response
const addProduct = async (
{ request, response }: {
request: Request;
response: Response;
},
)您可以看到Oak Response有一个方法toServerResponse,该方法将Oak的响应转换为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> {https://stackoverflow.com/questions/62533836
复制相似问题