【问题标题】:Express 4 server crashes right after loading statics filesExpress 4 服务器在加载静态文件后立即崩溃
【发布时间】:2015-01-27 20:26:46
【问题描述】:

我的本​​地机器上有工作代码,但在服务器 (Digital Ocean) 上运行相同的代码时遇到问题。我有新的/干净的机器,节点 v0.10.36 没有运行其他服务,没有占用/使用其他端口。

运行npm start 或node bin/www 后,我看到该服务器正在运行,但在我访问该站点后,服务器崩溃并出现以下错误:

root@video-dl:~/server# npm start

> server@0.0.0 start /root/server
> node ./bin/www

GET / 304 129.279 ms - -

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:1011:11)
    at Process.ChildProcess._handle.onexit (child_process.js:802:34)

npm ERR! server@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the server@0.0.0 start script.
npm ERR! This is most likely a problem with the server package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls server
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 3.13.0-43-generic
npm ERR! command "/root/.nvm/v0.10.36/bin/node" "/root/.nvm/v0.10.36/bin/npm" "start"
npm ERR! cwd /root/server
npm ERR! node -v v0.10.36
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /root/server/npm-debug.log
npm ERR! not ok code 0
root@video-dl:~/server#

【问题讨论】:

    标签: node.js express production-environment digital-ocean


    【解决方案1】:

    看hereENOENT表示文件或目录不存在。

    大多数情况下,您可以通过在静态文件夹定义中添加绝对路径来解决此问题。

    app.use(express.static(__dirname + '/public'));
    

    要检查文件夹是否存在,您可以使用真实路径:

    console.log(fs.realpathSync(__dirname + '/public');
    

    【讨论】:

    • 当进程没有读取权限时,在某些操作系统上可能会出现同样的错误
    • 我将app.use(express.static(path.join(__dirname, 'public'))); 更改为app.use(express.static(__dirname + '/public')); => 再次在本地主机上工作,但在服务器上崩溃。控制台日志显示当前路径,我确定文件存在。
    • 服务器上的代码在到达真实路径日志之前就崩溃了——不管我把日志放在哪里。
    • 所以现在我自己尝试了...寻找具有不存在路径的真实路径让我的应用程序崩溃...与您最初发布的完全相同的问题。 var fs = 需要('fs'); console.log(fs.realpathSync('./fdgfd/dsfgsda/'));
    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2016-02-16
    • 2020-07-01
    相关资源
    最近更新 更多