my question is how to update ejs template and url path when we have GETrequest from the client. Now I'm doing it in that way:
Server routing:
router.get('/', function(req, res, next) {
res.redirect('/cat/');
});
router.get('/cat', function(req, res, next) {
var files = require('../lib/treeBuilder').renderDir('./');
res.render('index', { title: 'FileBrowser', dir: '/', files: files });
});
router.get('/cat/:file*', function(req, res, next) {
var path = req.path.slice(4);//get path without 'cat'
console.log('IN "/:file" >' + path);
var files = require('../lib/treeBuilder').renderDir('./' + path);
res.render('index', { title: 'FileBrowser', dir: path, files: files });
});
Client js
_sendXMLHTTPRequest(folder) {
var xhr = new XMLHttpRequest();
xhr.open('GET', `${window.location.pathname}${folder}`, true);
xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
if (this.status != 200){
return alert('Error xhr!');
}
}
//xhr.send();//it must be correct way
window.location.href += `${folder}/`;// it's my way
}
I'm doing something like file browser - we have some list of dirs on /cat/ page - user clicks on one of them - it sends a request to server to show it. Click on lib dir - go to cat/lib/ - render template.
As you can see, I manually change window.location.href instead of make xhr.send() - it's all because of xhr.send() doesen't changes URL, so my EJS template doesen't update. Thanks for help.
Aucun commentaire:
Enregistrer un commentaire