Search code examples
routesframeworksdenoopine

Deno - opine router. How to read values from page?


I'm working as a beginner with Deno and the Opine framework. The first page is for login, but I can't read values of the form fields.

Form :

<form action="/signin" method="POST" style="margin-top:30px;">
    <input type="text" name="user" placeholder="Username" required="required" class="input-txt" value=""/>
    <p>&nbsp;</p>
    <input type="password" name="password" placeholder="Password" required="required" class="input-txt" />
    <div class="login-footer">
        <button type="submit" class="btn btn--right">Sign in  </button>
    </div>
</form>

Router in controller - my main problem is HERE. Can't understand how to read values of fields :

index.ts :

import { Router } from 'https://deno.land/x/opine/mod.ts';
const router = new Router();
router.post("/signin", async ( req, res ) => 
{
  console.log( "here... ???" ); 
})
export default router;

In server.ts:

import signin from "./controller/index.ts";
...
app.use( "/", signin );

I found many samples with OAK framework. In the post they use like that :

(ctx) => {
   const form = ctx.request.body ...

But the Opine framework seems to work in different way.


Solution

  • First of all, I added the "use json" to my app[1]:

    const app = opine();
    const port = 3000;
    app.use(urlencoded());
    

    I then I parse the parameters here[2]:

    Users.post("/", async (req, res) => {
      console.log(req.parsedBody.user)
      console.log(req.parsedBody.password)
    }
    

    Links for my personal project:

    1 - https://github.com/ramonmedeiros/learning_deno/blob/master/opine/app.ts#L11

    2 - https://github.com/ramonmedeiros/learning_deno/blob/master/opine/controllers/users.ts#L26-L54