【问题标题】:Why is html route not working in express.js?为什么 html 路由在 express.js 中不起作用?
【发布时间】:2019-04-28 00:35:26
【问题描述】:

所以这是一个非常微不足道的问题...... 我的客户端代码中有一个获取请求。

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

后面还有一个听众

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

但是,它不起作用。如果你直接去 /survey ,它确实有效,但是 get 请求也指向 /survey 并且应该在 onclick 发生,因为它绑定到前面的 click 事件。

【问题讨论】:

标签: javascript node.js express


【解决方案1】:

这解决了它。

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

【讨论】:

  • 其实,对于公共静态内容,最好使用serve-static中间件,这似乎是该路线的目标。
  • 对您所做更改的一些解释以及为什么会对以后找到您答案的人有所帮助。
  • const serveStatic = require('serve-static'); ...然后稍后,您会执行以下操作:app.use('/survey', serveStatic('public'))
  • serve-static 已被使用。这个问题不同(与静态服务无关)
  • 基本上我只是想(在一天结束时)在最后没有 html 的漂亮类型的 url
猜你喜欢
  • 2019-09-12
  • 1970-01-01
  • 2016-11-23
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 2021-08-14
相关资源
最近更新 更多