加载静态文件后,Express 4服务器立即崩溃

时间:2015-01-27 20:26:46

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

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

运行npm startnode 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#

1 个答案:

答案 0 :(得分:1)

查看here ENOENT表示文件或目录不存在。

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

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

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

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