简体   繁体   English

为什么 html 路由在 express.js 中不起作用?

[英]Why is html route not working in express.js?

So it's a pretty trivial question... I have a get request in my client code.所以这是一个非常微不足道的问题......我的客户端代码中有一个 get 请求。

function tosurvey(){$.get( "/survey", d=>d?console.log("Redirected"):false);}

And a listener on the back和后面的听众

 router.get('/survey', function(req, res) {
        res.sendFile('survey.html', { root: path.join(__dirname, '../public') });
    });

However, it doesn't work.但是,它不起作用。 If you go directly to /survey, it does work, but get request is pointing to /survey too and should be happening onclick as it is binded to click event on the front.如果您直接转到 /survey,它确实有效,但是 get 请求也指向 /survey,并且应该在 onclick 上发生,因为它绑定到前面的 click 事件。

This fixed it.这修复了它。

    let x = window.location.pathname;
    x=x.split("/");
    x.pop();
    x=x.join('/');
   $.get("/", d=>d?(console.log("Redirected"),window.location=x+'/'):false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM