我想在 deno 中强类型化 Oak 提供的 context.state 对象。我已经看到了如何实现这一点的方法(例如Deno Oak v10.5.1 context.cookies never be set),但还没有设法在我自己的代码中实现它。
我的目标是访问每个中间件中的强类型 context.state。
这是 context.state 的接口:
interface State {
userID: number;
sessionID: number;
}
Run Code Online (Sandbox Code Playgroud)
这将是我用于设置 context.state 的中间件函数之一,我在其中尝试访问上下文状态属性:
const contextMiddleware = async (context: Context, next: () => Promise<unknown>): Promise<void> => {
context.state.userID = 123;
context.state.sessionID = 456;
await next();
delete context.state.userID;
delete context.state.sessionID;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在 contextMiddleware 函数中,这两个属性的类型为any,而不是预期的数字类型。此外,智能感知无法识别这些自动完成属性。
我发现解决方案可能是将 IState 接口作为泛型传递给调用中间件的 Application 和 Router 对象,然后设置 contextMiddlewate 函数以使用来自 Oak import mod.ts 的相同泛型键入 RouterMiddleware 或 Middleware。
这可能看起来像这样,但目前不起作用:
import {
Application, …Run Code Online (Sandbox Code Playgroud) 我为 Deno POC 编写了几个 api。
这是路线代码:
const router = new Router()
router.get('/posts', getPosts)
.get('/posts/:id', getPostsById)
Run Code Online (Sandbox Code Playgroud)
对于第二条路线,我可以使用关键字:params 在控制器中获取路径参数:getPostsById。这是控制器代码:
export const getPostsById = (
{ params, response }: { params:any, response: any }) => {
console.log(params, '||| params')}
Run Code Online (Sandbox Code Playgroud)
如何以类似的方式获取查询参数(例如:/posts/2222?userId=3)
我使用 Oak 进行路由。我尝试了 Oak 代码库中的各种关键字:查询、搜索等,但没有成功。
我也尝试了 Oak 文档中的 getQuery,但我完全无法导入它。
我见过:How can I return html or json with deno? 但我需要橡树的答案 - 下面发布。
我一直在努力在 deno(oak 服务器框架)中实现 404 页面 - 如果我加载任何不存在的地址,我只会得到一个空白页面..
尝试过:(page404MiddleWare.ts):
import {Context, exists, send} from "./deps.ts";
export const page404MiddleWare = async (ctx: Context, next: Function) => {
ctx.response.body = "404 page";
await next();
}
Run Code Online (Sandbox Code Playgroud)
但这似乎是一种不好的做法。
我习惯使用 NodeJS 和 Koa。我一直在使用 Deno 并运行了静态文件服务器的示例:
/* static_server.js */
import { Application } from 'https://deno.land/x/oak/mod.ts'
const port = 8080
const app = new Application()
// Error handler middleware
app.use(async (context, next) => {
try {
await next()
} catch (err) {
console.error(err)
}
})
// Send static content
app.use(async (context) => {
console.log(`${context.request.method} ${context.request.url.pathname}`)
await context.send({
root: `${Deno.cwd()}/static`,
index: "index.html",
})
})
await app.listen({ port })
Run Code Online (Sandbox Code Playgroud)
我还使用路由创建了一个动态服务器:
/* routes.js */
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
const port = …Run Code Online (Sandbox Code Playgroud) 我用的是橡木/德诺。我有一个从提供的 ejs 文件提交的表单。如何访问表单主体?当我将其记录到控制台时,它会打印:{type: "form", value: URLSearchParamsImpl {} }
帖子处理程序如下所示:
router.post("/add", async (ctx: RouterContext) => {
const body = (await ctx.request.body())
console.log(body)
ctx.response.redirect("/");
});
Run Code Online (Sandbox Code Playgroud) 我在学习 Deno 和 Oak 方面取得了缓慢但稳定的进展,但这让我感到困惑。我有一个带有文件上传字段的简单网络表单:
<form method="post" action="/quote" enctype="multipart/form-data">
<label>Author:
<input type="text" name="author" />
</label>
<label>file: <input type="file" name="myfile" multiple />
</label>
<label>Quote:
<textarea name="quote"></textarea>
</label>
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
处理是通过 Deno 和 Oak 完成的,下面是处理文本数据的脚本:
router.post('/quote', async context => {
const body = context.request.body({ type: 'form' })
const value = await body.value
const author = value.get('author')
console.log(author)
context.response.redirect(`/?author=${author}`)
})
Run Code Online (Sandbox Code Playgroud)
该路由可以处理没有编码的表单multipart/form-data,但一旦我添加该编码,该author字段就是undefined。
我的问题是:如何访问此表单中的数据(文本和文件数据)?