ExpressJS change route without refresh

2019-08-27 17:40发布

问题:

Hey I have a website run with Express.js and Handlebars that has a navbar and sidebar that stays the same for all the pages. When I route it only changes the page container which is {{{body}}}.

Is there a way I can just load the content of the routes without the page actually refreshing and reloading the nav and sidebar?

I'm currently using this for routing.

router.get('/', checkAuthentication, function(req, res){
  res.render('index');
});

Any help would be nice as I'm quite new with node.

I want only the container to load not the whole url/page

回答1:

you can send the data you need to the client using:

res.send(JSON.stringify(Obj);

and in the ajax success function you need to:

JSON.parse(dataSentFromServer);

and render the data using handlebars in the success function or another function that you call



回答2:

Thank you all for considering/looking at the post, i have come to the conclusion to be using PJAX to do this.

The tutorial i read, for anyone wondering is : Pjax Tutorial