Nodejs服务器停止

时间:2014-02-24 15:29:57

标签: node.js express amazon-ec2

我在运行ExpressJS / Nodejs的Amazon Ec2(m1.large)上有一台服务器。这个我的服务器每秒接收2.000个请求,但是一段时间后nodejs单独停止..在我的浏览器中显示“此网页不可用”.. nodejs不支持几个请求?或者它可以是我的服务器配置?

app.js

/**
 * Module dependencies.
 */

var express = require('express');
var mysql   = require('mysql');
var routes  = require('./routes');
var verify  = require('./routes/verify');
var http    = require('http');
var path    = require('path');






pool = mysql.createPool({
  host     : 'rds.amazonaws.com', //old db
  user     : '123123',
  password : '123123',
  database : '123'
});



var app = express();

// all environments
app.set('port', process.env.PORT || 8080); //3000, 8080
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.cookieParser());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}



app.get('/', routes.index);
app.get('/l.node', verify.checkin); //l.cpp?q=31&w=c
app.get('/crossdomain.xml', verify.crossdomain); 

http.createServer(app).listen(app.get('port'), function(){
  console.log('@Express server listening on port ' + app.get('port'));

});

verify.js

/*
|--------------------------------------------------------------------------
| CROSSDOMAIN.XML
|--------------------------------------------------------------------------
*/
exports.crossdomain = function (req, res){

  res.set('Content-Type', 'text/xml; charset=utf-8');
  res.send('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" /></cross-domain-policy>');

};





















/*
|--------------------------------------------------------------------------
| BANNER CHECK-IN
|--------------------------------------------------------------------------
*/
exports.checkin = function(req, res){
    banner_id = req.query.q;
    type_req  = req.query.w;






        updateBannerView(banner_id, type_req, true);


        if(type_req == 'c'){
            updateBannerClick( banner_id, res);
        } else {
            res.send('commit range ' + banner_id + '..' + type_req + '; l:');
        }






}; //end exposts













/*
|--------------------------------------------------------------------------
| ADD CLICK BANNER
|--------------------------------------------------------------------------
*/
var updateBannerClick = function (p_banner_id, p_res) {
    var link = '';

  pool.getConnection(function(err, connection) {


    connection.query("UPDATE  `reportbanners` SET  `clicks` = `clicks`+1 WHERE `date` = ? AND `banner_id` = ?", [getDateTime(), p_banner_id], 
        function(err,rows,field){
     });

    connection.query('SELECT * FROM  `banners` WHERE  `id` = ?', [p_banner_id], function(err, rows, fields) {
        if(rows.length != 0){
            link = rows[0].banner_clicklink;
            p_res.send('<script>window.location = "' + link + '";</script>');
            console.log("Link Active");
        } else {
            console.log("Error Link Active");
            p_res.send('<script>window.location = "http://www.google.com.br";</script>');
        }

    });


    connection.release();
  });//end pool


};















/*
|--------------------------------------------------------------------------
| ADD VIEW BANNER
|--------------------------------------------------------------------------
*/
var updateBannerView = function (p_banner_id, p_type_req, p_isSessionViewUpdate) {
pool.getConnection(function(err, connection) {

            console.log(err);
    var getQuery = "";
    connection.query('SELECT * FROM  `reportbanners` WHERE  `date` = ? AND `banner_id` = ?', [getDateTime(), p_banner_id], function(err, rows, fields) {


      if(rows.length == 0){



        if(p_isSessionViewUpdate){
            getQuery = "INSERT INTO `reportbanners` (`id` , `date` , `clicks` , `views` , `session_views` , `banner_id` ) VALUES (NULL ,  NOW(),  '0',  '1',  '1',  ?)";
        } else {
            getQuery = "INSERT INTO `reportbanners` (`id` , `date` , `clicks` , `views` , `session_views` , `banner_id` ) VALUES (NULL ,  NOW(),  '0',  '1',  '0',  ?)";
        }

        connection.query(getQuery, [p_banner_id], 
            function(err,rows,field){

            });
        // debug
        console.log("INSERT REG");




      } else {




        if(p_isSessionViewUpdate){
            getQuery = "UPDATE  `reportbanners` SET  `views` = `views`+1, `session_views` = `session_views`+1 WHERE `date` = ? AND `banner_id` = ?";
        } else {
            getQuery = "UPDATE  `reportbanners` SET  `views` = `views`+1 WHERE `date` = ? AND `banner_id` = ?";
        }

        connection.query(getQuery, [getDateTime(), p_banner_id], 
            function(err,rows,field){

            });

        console.log("ATT REG");



      };






      console.log('Results: ', rows.length);
      console.log('date: ', getDateTime());


      // console.log('s: ', the_banner_link);
    });





connection.release();
});//end pool
};









var getDateTime = function () {

    var date = new Date();

    var hour = date.getHours();
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" : "") + month;

    var day  = date.getDate();
    day = (day < 10 ? "0" : "") + day;

    // return year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
    return year + "-" + month + "-" + day;

}

1 个答案:

答案 0 :(得分:0)

我怀疑:

(1)您正在从终端调用节点应用程序,终端关闭时应用程序将被终止。或

(2)您的脚本可能在某个时间点失败。或

(3)您的服务器重新启动。

每个案例的解决方案:

(1)使用nohupnohup node server.js &

(2)使用forever运行您的应用。 forever start server.js

(3)配置系统以在系统启动时运行您的应用程序。例如。使用upstart