【问题标题】:NodeJitsu error: Error: ENOENT, stat '/opt/run/snapshot/NodeJits 错误:错误:ENOENT, stat '/opt/run/snapshot/
【发布时间】:2015-01-05 17:08:57
【问题描述】:

我的应用程序的 NodeJits 文件夹结构如下(即,当我执行 jitsu deploy 时,我位于包含“server.js”的文件夹中 - 即“server”文件夹)。

Root    server
        |___server.js
        |___package.json
        client
        |___www
            |___index.html
                |___css
                |___js
                |___etc.

因此,根目录是“server”文件夹,其中包含启动脚本“server.js”。然后是一个名为“client”的文件夹,与“server”平行,其中有一个名为“www”的文件夹,在“www”中是主要的“index.html”。

在我的“server.js”文件中,我有以下代码:

app.get(‘/’, function(req,res)
{
    var aPath = path.resolve(“../client/www/”, “index.html”);
    res.sendFile(aPath);
});

我没有app.use(express.static(__dirname + '/somefolder')。当我启动应用程序时,我收到此错误:

Error: ENOENT, stat '/opt/run/snapshot/client/www/index.html'

我的process.cwd()/opt/run/snapshot/package。显然,上面的路径并没有指向“index.html”所在的位置。但我认为我执行path.resolve(…) 的方式应该指向“index.html”。我看不出问题出在哪里。如果“server.js”在根目录下,那么要到达“index.html”,它在“client/www/index.html”中,那么我需要写“../client/www”,相对到执行脚本,到“index.html”,对吧?

您对路径设置不正确有任何见解吗? /opt/run/snapshot/ 应该指向什么?或者,我需要在get(‘/’) 处理程序中进行哪些更改才能正确指向我的“index.html”?

编辑

我错误地绘制了文件夹结构。现在是正确的。

我还关闭了app.get() 并打开了app.use(express.static(__dirname + '/../client/www/')。但无济于事:现在我收到Cannot GET / 错误。

我最终想要的是让“server.js”文件成为 Node 服务器,它主要只是将 AngularJS HTML 文件提供给浏览器,附带来自“客户端”的图像、样式表等文件夹。这是服务器的主要角色,还有一个额外的角色是验证应用程序的用户,使用非常好的 Satellizer 模块。就是这样。我连接了一个 MongoDB,但除此之外,这是一个非常常见且直接的 Node.js 服务器应用程序。

谢谢!

【问题讨论】:

  • 它在本地工作吗?您的 CLIENT 和 WWW 大写,确保不是大小写问题。

标签: node.js express nodejitsu


【解决方案1】:

尝试不生根,解决并注销以仔细检查:

// notice no leading / which is root.  __dirname should be the dir of current file running
var staticPath = path.resolve(__dirname, '../client/www');
console.log(staticPath);

然后将其传递给 express.static

app.use(express.static(staticPath);

我可能会建议遵循快速生成的应用程序的布局和约定,应用程序位于根目录中,静态文件位于公共目录下

/public
   <static files>
app.js

然后执行生成的应用程序的操作:

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

【讨论】:

  • 好的,谢谢,bryanmac;我会尝试你的建议。欣赏!
  • 在这些更改之后,__dirname 为“/opt/run/snapshot/package”,并且在您创建它时使用 staticPath,新路径变为 staticPath='/opt/run/snapshot/client /www' 但我认为它应该是 staticPath='/opt/run/snapshot/package/client/www'。但是 - 它有效!所以,再次非常感谢。感谢您的时间。
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 2014-12-23
  • 2015-10-10
  • 2015-11-24
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多