Dynamically loading webpage using node/express, need to render on the same page

喜欢而已 提交于 2019-12-02 08:01:19

If you are trying to convert your app to use a single page architecture, while still pre rendering all of your HTML on the server, you will need to implement some form of a client side router like this one. In your client-side code, within the callback for each route, you can make an AJAX call to your server, and then replace the entire html document with the html the server returns. Something like this if you are using jquery:

route('/login', function(name) {
      $.get("/login", {
            anyotherparamsyouwanttosend: "whatever",
        },
        function(htmlResponse) {
            $('html').html(htmlResponse);
        })
})

But there's not much of an advantage to this approach over a traditional multi-page architecture. A better approach would be to render the mustache templates on the client side. In your Express code you would use res.json instead of res.render to send just the data you need to render the Mustache template on the client side instead of sending a big block of HTML.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!