deno 视图引擎和 Oak 之间的 http 服务器冲突

发布于 2025-01-10 09:06:05 字数 1916 浏览 0 评论 0原文

我想用视图引擎和 Oak 渲染一些 html 页面,但也能够将数据发送到 postgresql 数据库。

首先,在构建我的代码时,我能够渲染 ejs 页面。另一方面,我注入了允许将数据发送到数据库的代码,它也起作用了。

但是,当我希望两者都操作时,它不起作用(在邮递员上收到 404 错误):

这是没有查看路由和查看模块的代码,只是使用 router.post 将数据发送到数据库:

没有查看模块的 server.ts和查看路由(此代码有效):

import { Application} from "https://deno.land/x/oak/mod.ts";
import { send } from "https://deno.land/x/oak/send.ts";

import {
    viewEngine,
    engineFactory,
    adapterFactory,
  } from "https://deno.land/x/view_engine/mod.ts";

import router from './routes.ts';

const ejsEngine =  engineFactory.getEjsEngine();
const oakAdapter =  adapterFactory.getOakAdapter();

const port = Deno.env.get("PORT") || 5000
const app = new Application();

/*app.use(viewEngine(oakAdapter, ejsEngine));
    app.use(async(ctx,next) => {
  await send(ctx,ctx.request.url.pathname,{
      root: `${Deno.cwd()}`
  });
  next();
});*/



app.use(router.routes());
app.use(router.allowedMethods());




//deno run --allow-net --allow-read --allow-env server.ts
 
 
console.log(`Server running on port ${port}`);

await app.listen({ port: +port })

不带视图模块的routes.ts和查看路由:

import { Router } from "https://deno.land/x/oak/mod.ts";
import {addUser} from './controllers/products.ts';

  
import {ejsVariables} from './EJS/ejsvariables.js';
//import {showDate} from './EJS/showdate.js';


  
const router = new Router();


router.post('/OBV/compteclient', addUser);

/*router.get('/OBV/acceuil',(ctx:any)=>{
    ctx.render('./OBV/acceuil/OBV.ejs');
});*/


/*router.get('/OBV/compteclient',(ctx:any)=>{
  ctx.render('./OBV/compteclient/CompteClient.ejs');
});*/

/*router.get('/OBV/boutique',(ctx:any)=>{
  ctx.render('./OBV/boutique/Boutique.ejs',
  ejsVariables.dataUser); 
})*/



 export default router

server.ts代码的注释部分是否存在异步问题?

I want to render some html pages with view engine and oak but also be able to send data to postgresql database.

First in the construction of my code, i was able to render ejs pages. in an other hand, i injected the code that permit to send data to database, it also worked.

But when i want the both to operate it don't work (getting a 404 error on postman) :

Here is the code without view routes and view module, just sending data to database with a router.post :

server.ts without view module and view routes (this code works) :

import { Application} from "https://deno.land/x/oak/mod.ts";
import { send } from "https://deno.land/x/oak/send.ts";

import {
    viewEngine,
    engineFactory,
    adapterFactory,
  } from "https://deno.land/x/view_engine/mod.ts";

import router from './routes.ts';

const ejsEngine =  engineFactory.getEjsEngine();
const oakAdapter =  adapterFactory.getOakAdapter();

const port = Deno.env.get("PORT") || 5000
const app = new Application();

/*app.use(viewEngine(oakAdapter, ejsEngine));
    app.use(async(ctx,next) => {
  await send(ctx,ctx.request.url.pathname,{
      root: `${Deno.cwd()}`
  });
  next();
});*/



app.use(router.routes());
app.use(router.allowedMethods());




//deno run --allow-net --allow-read --allow-env server.ts
 
 
console.log(`Server running on port ${port}`);

await app.listen({ port: +port })

routes.ts without view module and view routes :

import { Router } from "https://deno.land/x/oak/mod.ts";
import {addUser} from './controllers/products.ts';

  
import {ejsVariables} from './EJS/ejsvariables.js';
//import {showDate} from './EJS/showdate.js';


  
const router = new Router();


router.post('/OBV/compteclient', addUser);

/*router.get('/OBV/acceuil',(ctx:any)=>{
    ctx.render('./OBV/acceuil/OBV.ejs');
});*/


/*router.get('/OBV/compteclient',(ctx:any)=>{
  ctx.render('./OBV/compteclient/CompteClient.ejs');
});*/

/*router.get('/OBV/boutique',(ctx:any)=>{
  ctx.render('./OBV/boutique/Boutique.ejs',
  ejsVariables.dataUser); 
})*/



 export default router

Is there an asynchronous problem with the commented part of the server.ts code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2025-01-17 09:06:05

所以,我发现出了什么问题,这是关于 server.ts 中 app.use 的顺序,路由器被永远无法到达的等待发送所掩盖。

app.use(viewEngine(oakAdapter, ejsEngine));

app.use(router.routes());
app.use(router.allowedMethods());

app.use(async(ctx,next) => {
  await send(ctx,ctx.request.url.pathname,{
      root: `${Deno.cwd()}`
  });
  next();
});

So, i found out whats wrong, it was about app.use's order in server.ts,routers were getting masked by the await send that was never reach.

app.use(viewEngine(oakAdapter, ejsEngine));

app.use(router.routes());
app.use(router.allowedMethods());

app.use(async(ctx,next) => {
  await send(ctx,ctx.request.url.pathname,{
      root: `${Deno.cwd()}`
  });
  next();
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文