【发布时间】:2020-08-05 16:25:31
【问题描述】:
我正在使用 Deno v1.2.1
我一辈子都不能渲染模板,我可以很容易地写回复,但渲染是另一回事,我得到undefined is not a function
// Requiring modules
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import {viewEngine, engineFactory, adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
// Initiate app
const app = new Application();
const router = new Router();
// Setting up boilerplate for view-engine
const renderEngine = await engineFactory.getDenjuckEngine();
const oakAdapter = await adapterFactory.getOakAdapter();
// Adding middleware to require our router
app.use(
viewEngine(oakAdapter, renderEngine, {
viewRoot: `${Deno.cwd()}`
})
);
// Creating Routes
router.get("/",(ctx:any, next)=>{
ctx.render('index.html') // This gives undefined is not a function
//ctx.response.body = "hello"; // This works,
});
app.use(router.routes());
app.use(router.allowedMethods());
// Making app to listen to port
console.log('App is listening to port: 55555');
await app.listen({port:55555});
当我运行代码时,我得到以下信息,我不确定它是否相关
Warning Failed to get compiled source code of "https://deno.land/std@0.59.0/path/mod.ts".
我正在运行这样的代码:
deno run --allow-net=0.0.0.0:55555 --allow-read main.ts
我的文件夹结构是:
_base.html
index.html <- inherits from _base.html
main.ts
有人指点吗?
2020 年 8 月 5 日 虽然以下内容曾经可以工作,但 Deno 更新导致它停止工作,非继承模板和从基本文件继承的模板会产生“未定义不是函数”
/_base.html /index.html
<!--_base.html-->
<html>
{% block content %}{% endblock content %}
</html>
<!--index.html-->
{% extends "_base.html" %}
临时解决方案 2020 年 7 月 25 日
在 cmets 的帮助下,我有了这段代码,它可以工作,但是如果没有将它们硬编码到路由中,静态文件夹将无法工作:
// Requiring modules
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import {viewEngine, engineFactory, adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
// Initiate app
const app = new Application();
const router = new Router();
// Setting up boilerplate for view-engine
const renderEngine = await engineFactory.getDenjuckEngine();
const oakAdapter = await adapterFactory.getOakAdapter();
app.use(viewEngine(oakAdapter, renderEngine));
router
.get("/", async (context:any, next) => {
context.render('index.html')
}).get("/upload", async (context:any, next) => {
context.cookies.set(
"lastVisit",
new Date().toISOString(),
{ httpOnly: true, sameSite: "strict" },
);
context.render('upload.html')
}).get("/images/:file", async (context) => {
const file = context.params.file;
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}`,
index: file,
});
}).get("/css/:file", async (context) => {
const file = context.params.file;
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}`,
index: file,
});
}).get("/js/:file", async (context) => {
const file = context.params.file;
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}`,
index: file,
});
});
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener('error', e => {
console.log(e.error);
});
app.addEventListener('listen', ({hostname, port, secure}) => {
console.log(`Listening on ${secure ? 'https://' : 'http://'}${hostname || 'localhost'}:${port}`)
});
// Making app to listen to port
await app.listen({port:55555});
【问题讨论】:
标签: typescript deno