deno http server conflict between view engine and oak

140 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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();
});