简体   繁体   English

为什么我不能在屏幕上或使用Forever运行node.js&socket.io?

[英]Why can't I run node.js & socket.io in a screen or with Forever?

I've got some annoying problem and that is I have trouble in running nodejs in a screen. 我有一些恼人的问题,那就是我在屏幕上运行nodejs时遇到了麻烦。 Because as fast as I leave the screen and no sockets are connected the next whom is to connect will be faced by an error message until the screen is opened again. 因为只要我离开屏幕并且没有连接插座,下一个要连接的人将面临错误消息,直到屏幕再次打开。 As soon as the screen is open with screen -R node (for example) it will start accepting connections again. 一旦屏幕打开screen -R node (例如),它将再次开始接受连接。

However notice that if the screen is closed (running in the background) and someone already has a socket open, it will continue to answer new connections. 但请注意,如果屏幕关闭(在后台运行)并且某人已打开套接字,则它将继续回答新连接。

When I try to start the application again, with the command node app I get the following message: 当我尝试再次启动应用程序时,使用命令node app我收到以下消息:

module.js:340
    throw err;
          ^
Error: Cannot find module '(unreachable)/square_defense/app'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Server side code : 服务器端代码

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function (req, res) {
  console.log(__dirname);
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
    socket.on('user-message', function (data) {
        console.log(data);
        sendMessage.call(socket, data.message);
    });
});
var sendMessage = function(message) {
      this.emit('server-message', {message: message});
      this.broadcast.emit('server-message', {message: message});
}

I have tried a lot of things like npm install app in directory .. from the app.js. 我在app.js的目录中尝试了很多像npm install app这样的东西.. Can't understand why it dont want to work. 无法理解为什么它不想工作。 I really just want a server which I can use for testing purposes that do not crash. 我真的只想要一个可以用于测试目的的服务器,不会崩溃。

EDIT: I think this has something to do with an encrypted home folder? 编辑:我认为这与加密的主文件夹有关?

I think the unreachable folder is where the problem resides. 我认为无法访问的文件夹是问题所在的位置。 Error: Cannot find module ' (unreachable) /square_defense/app'. 错误:找不到模块' (无法访问) / square_defense / app'。 But I don't know why this is happening. 但我不知道为什么会这样。 Right now I am running the application with Forever and as soon as I have an ssh-session on it will work. 现在我正在使用Forever运行应用程序,只要我有一个ssh-session就可以了。 But as soon as I exit the connection and reload the page. 但是一旦我退出连接并重新加载页面。 It will generate an error. 它会产生错误。

As you said, your home directory is encrypted, and according to the document from Ubuntu : 正如您所说,您的主目录已加密,并根据Ubuntu的文档

This directory is automatically mounted on login, and unmounted on logout. 此目录在登录时自动挂载,并在注销时卸载。

So your guess is correct, this is exact what's causing the error. 所以你的猜测是正确的,这正是造成错误的原因。 Running a detached screen session won't be considered by the OS as the user are still logged in*. 操作系统不会考虑运行分离的screen会话,因为用户仍然登录*。 The time all shell sessions of the user ends, the encrypted home directory got unmounted. 用户的所有shell会话结束的时间,加密的主目录已卸载。


Solution: You can move your app to a directory other than $HOME , then I think either GNU Screen or forever will do the job without errors.† 解决方案:您可以将应用程序移动到$HOME以外的目录,然后我认为GNU Screen永远都可以正常工作。†


* You can test this by your self: login, start a screen session, run something like vim , detach; *您可以通过自己测试:登录,启动screen会话,运行像vim ,detach; use command w to see who is logged in and what they are doing 使用命令w查看谁登录以及他们正在做什么

† I think if you write your own upstart scripts for this, it is still crucial that the app files are not placed in auto-encrypted home directory. †我认为如果您为此编写自己的新手脚本,那么将应用程序文件放在自动加密的主目录中仍然至关重要。

Since you already are on an Ubuntu box, i recommend using Upstart and monit as described in this post: http://howtonode.org/deploying-node-upstart-monit 由于您已经在Ubuntu盒子上,我建议使用Upstartmonit,如本文所述: http//howtonode.org/deploying-node-upstart-monit

The only thing i added into the mix was a frontend proxy-server (HAProxy in my case, but nginx works as well) in order to serve my application on port 80. 我在混合中添加的唯一内容是前端代理服务器(在我的情况下是HAProxy,但nginx也可以),以便在端口80上为我的应用程序提供服务。

I deployed a socket.io/express app using this technology stack on Amazon EC2 myself about a month ago, with amazing results! 我大约一个月前在Amazon EC2上使用这个技术堆栈部署了一个socket.io/express app,效果惊人!

EDIT: 编辑:

Here are some resources i collected over time concerning a proper production setup using node.js/nginx/monit/git: 以下是我使用node.js / nginx / monit / git获取的关于正确生产设置的一些资源:

I really recommend the first one, you might have to skip some parts (like npm module installation or system setup), but the parts about nginx/git and supervisor are worth a read. 我真的推荐第一个,你可能不得不跳过一些部分(如npm模块安装或系统设置),但有关nginx / git和supervisor的部分值得一读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM