Rendering a View for a Route

In this lesson, we'll learn how we can use the EdgeJS Template Engine to render HTML views and send them back as the response for our routes. We'll also see how we can pass dynamic data into our views from our route handler.

Published
Jan 24
Duration
6m 29s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Join The Discussion! (8 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 2 months ago

    It would be cool to get some tutorials on using other output options, like pushing to a SPA or using a different templating engine.

    1

    Please sign in or sign up for free to reply

    1. Commented 2 months ago

      Agreed, and thank you for the suggestion! It'd be good to cover those things, and we will most certainly get to them! It won't be within this series though, as we want this series to be a good entry point for everyone coming to AdonisJS 6, and using EdgeJS is the most inclusive way to do that as it's a part of the framework.

      We'll plan and add series for these into our schedule!

      0

      Please sign in or sign up for free to reply

      1. Commented 2 months ago

        Thanks Tom. I imagine you also will be updating a lot of your v5 lessons to v6. I really like your style. I find you way easier to understand than most tutorialists

        1

        Please sign in or sign up for free to reply

        1. Commented 2 months ago

          Yeah, I've got quite a bit planned including additional focus on project-based series that'll cover updated topics we covered in v5! I'll be putting 100% of my focus on this series until we wrap it up, then we'll move onward from there 😄

          Also, thank you very much that's very kind!! 😊

          0

          Please sign in or sign up for free to reply

          1. Commented 2 months ago

            Hi Tom, can you tell me why when I inspect the request object the parsed url host, protocol, etc are always null? I'm not sure the difference between host and hostname (if they were not null I could figure it out haha), but I want to be able to key off of that value in my code. I could grab the host header and parse that, but I'd rather just use the parsedUrl property for everything.

            
              "parsedUrl": {
                "protocol": null,
                "slashes": null,
                "auth": null,
                "host": null,
                "port": null,
                "hostname": null,
                "hash": null,
                "search": null,
                "query": null,
                "pathname": '/welcome',
                "path": '/welcome',
                "href": '/welcome'
            1

            Please sign in or sign up for free to reply

            1. Commented 2 months ago

              It looks like AdonisJS is using the request.url to parse these details out using the parse method from node:url and it looks like the request.url only contains the path for the request. So, it's being built without those details included and since the parsedUrl object is just an instance of URL that'd be why they're still included despite being null.

              You can get a complete parse of the URL by doing:

              router.get('/my-endpoint', (ctx) => {
                // passing true to completeUrl will include the query string
                const completeUrl = new URL(ctx.request.completeUrl(true))
                return completeUrl
              })
              Copied!

              Host and hostname will mostly be the same. The difference is, if the URL contains a port, host will include it and hostname will not.

              REQUEST: https://test.com:3333/endpoint
              HOST: test.com:3333
              HOSTNAME: test.com
              0

              Please sign in or sign up for free to reply

              1. Commented 2 months ago

                Thanks! I followed the link to the source code and it looks like all I have to do to get what I want is request.hostname(), and that method just pulls the info out of the headers anyway.

                0

                Please sign in or sign up for free to reply

                1. Commented 2 months ago

                  Anytime! Yep, that should do it! 😊

                  0

                  Please sign in or sign up for free to reply

Playing Next Lesson In
seconds