【问题标题】:404 Error: ENOENT, stat '/public/ when starting app from grunt404 错误:ENOENT,stat '/public/ 从 grunt 启动应用程序时
【发布时间】:2015-02-18 23:23:24
【问题描述】:

我正在运行一个快速应用程序,它尝试使用 sedfile 提供静态 html

res.sendfile('/public/testform.html');

当我使用 node app.js 启动应用程序时,它可以正常工作。当我尝试使用 grunt 启动应用程序时,它不起作用,它给了我错误

404 错误:ENOENT,stat '/public/testform.html'

我都试过了

  res.sendfile(__dirname+'/public/testform.html');
  res.sendfile(path.resolve('/public/testform.html'));

但我仍然没有从 grunt 那里得到运气。我究竟做错了什么 ?

【问题讨论】:

    标签: express routing gruntjs


    【解决方案1】:

    可能与快速静态文件服务有关。

    只需将其添加到您的 app.js 中:

    app.use('/public',express.static(path.join(__dirname, 'path-to-public/public')));
    

    app请注意我指的是:var app = express();,请注意中间件的顺序。

    【讨论】:

      【解决方案2】:

      我不知道为什么,但我能够让它工作的唯一方法是以下

      app.get('/testform', function (req, res) {
        var correctPath = path.join(__dirname, '..','/public/testform.html');
        res.sendfile(daPath);
      });
      

      __dirname inside routes 比它应该在的位置低一级,所以我不得不向上移动一级。

      【讨论】:

        猜你喜欢
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        相关资源
        最近更新 更多