nodejs应用程序没有收到请求

时间:2017-07-07 10:45:18

标签: node.js nginx

我正在尝试在端口5000上运行nodejs rest api应用程序,而另一个nodejs已经在另一个端口8080上运行了api。在端口5000上启动应用程序后。端口8080上的应用程序正在运行它是为了早,但是端口5000上的应用程序没有收到任何请求,我的客户端总是有时间。我正在为这两种情况创建https服务器。为什么会这样。

我正在运行nginx服务器。

我的应用代码:

const os = require('os');
var cluster = require('cluster');

const numCPUs = os.cpus().length;


 if (cluster.isMaster) {
  console.log('Master '+process.pid+' is running');

  // Fork workers.
 for (var i = 0; i < numCPUs; i++) {
  cluster.fork();
 }

 cluster.on('error', function(worker){
    console.log('Worker ' + worker.id + ' died..');
    cluster.fork();
 });

 cluster.on('exit', function(worker, code, signal){
  console.log('Worker ' + worker.id + ' died..');
  cluster.fork();
 });

} else {
const fs = require('fs');
const https = require('https');
const express    = require('express');        // call express
const app        = express();  
const bodyParser = require('body-parser');    // call body-parser
const addData = require('./dbhandler/addData');
const getData = require('./dbhandler/getData');
const updateData = require('./dbhandler/updateData');
const dbhandler = require('./dbhandler/dbhandler');
const log4js = require('log4js');
const request = require('request');
const redis = require('redis');

'use strict';
var nodemailer = require('nodemailer');

log4js.configure({
  "appenders": [ {
    "type": "dateFile",
    "filename": "./logs/apierrorlogs.log",
    "pattern": "-yyyy-MM-dd",
    "alwaysIncludePattern": false,
    "category" : "apilogs",
    "backup" : 3,
    "compress" : true
  } ]
});

var logger = log4js.getLogger("apilogs");
logger.setLevel('ERROR');

console.log('Worker '+process.pid+' started');

process.on('uncaughtException', function (err) {
  logger.error(err);
});


var privateKey  = fs.readFileSync('./abcdef.key', 'utf8');
var certificate = fs.readFileSync('./xyz.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const port = process.env.PORT || 5000;
const REDIS_PORT = process.env.PORT;
const redisClient = redis.createClient(REDIS_PORT);

function cache(req, res, next) {
    const key = req.url;
    // redisClient.del(key);
    redisClient.get(key , function (error, data) {
        if (error){
            console.log('error while getting data from cache for key = '+key)
            logger.error(error);
            next();
        } else if (data != null) {
            console.log('data is present in cache for key = '+key);
            res.send(JSON.parse(data));
        } else {
            console.log('data is not present in cache for key = '+key);
            next();
        }
    });
}

function checkUser(req,res){
    var success;
    try {
        var userExist;
        console.log("Route = /checkuser/:userEmail, userEmail = "+req.params.userEmail);
        getData.checkUserEmail(req.params.userEmail,function(error, results, fields){
            if (!error){
                if (results.length == 0) {
                    success = 1;
                    datasets = { 
                        "userExist" : 0
                    }
                    res.json({"success" : success, "datasets" : datasets});
                } else {
                    var userId = results[0]['user_id'];
                    getData.getMappedUserId(userId,0,function(error,results,fields){
                        if (!error) {
                            var userId = results[0]['id'];
                            getData.getUserInterests(userId, function(error, results, fields){
                                if (!error) {
                                    success = 1;
                                    datasets = {
                                        "userExist" : 1,
                                        "userId" : userId, 
                                        "interest" : results.length
                                    }
                                    res.json({"success" : success, "datasets" : datasets});
                                } else {
                                    logger.error("Route = /checkuser/, error = "+error);
                                    success = 0;
                                    res.json({"success" : success});
                                }
                            });
                        } else {
                            logger.error("Route = /checkuser/:userEmail, error = "+error);
                            success = 0;
                            res.json({"success" : success});
                        }
                    });
                }
            } else{
                logger.error("Route = /checkuser/:userEmail, error = "+error);
                success = 0;
                res.json({"success" : success});
            }
        });
    } catch (error){
        logger.error("Route = /checkuser/:userEmail, error = "+error);
        success = 0;
        res.json({"success" : success});
    }
}

router.get('/checkuser/:userEmail', checkUser);

app.use('/api/v1', router);

var httpsServer = https.createServer(credentials, app);
httpsServer.listen(port);

Process output in my terminal :
sasuke@example.com:~$ ps aux | grep node
sasuke    2140 37.3  3.3 1114124 270980 pts/0  Rl+  09:13  48:48 node 
/usr/bin/pm2 monit
sasuke    7031  0.1  0.3 915484 30972 ?        Ssl  10:31   0:05 node 
/home/sasuke/restapiv3/app-server-v3.js     
sasuke    7042  0.0  0.5 941300 45920 ?        Sl   10:31   0:03 node 
/home/sasuke/restapiv3/app-server-v3.js                
sasuke    7048  0.0  0.5 939936 46864 ?        Sl   10:31   0:03 node 
/home/sasuke/restapiv3/app-server-v3.js                
sasuke    7054  0.0  0.5 940824 47520 ?        Sl   10:31   0:03 node 
/home/sasuke/restapiv3/app-server-v3.js                
sasuke    7055  0.0  0.5 941320 45856 ?        Sl   10:31   0:03 node 
/home/sasuke/restapiv3/app-server-v3.js                
sasuke    8561  0.0  0.0  12008  2188 pts/4    S+   11:24   0:00 grep -
-color=auto node
sasuke   30629  0.2  0.4 922344 35524 ?        Ssl  Jul03  13:06 node 
/home/sasuke/restapi/app.js                 
sasuke   30639  0.3  1.1 1264668 93944 ?       Sl   Jul03  20:43 node 
/home/sasuke/restapi/app.js                            
sasuke   30645  0.3  1.0 1262676 89328 ?       Sl   Jul03  20:43 node 
/home/sasuke/restapi/app.js                            
sasuke   30651  0.3  1.1 1265152 92096 ?       Sl   Jul03  20:37 node 
/home/sasuke/restapi/app.js                            
sasuke   30652  0.3  1.0 1264576 89852 ?       Sl   Jul03  20:48 node 
/home/sasuke/restapi/app.js`

我的nginx配置文件:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 8096;
    multi_accept on;
}

worker_rlimit_nofile 40000;

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 45;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # buffer optimizations
    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

    ##
    # Logging Settings
    ##

    #access_log /var/log/nginx/access.log;
    access_log off;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
 }

0 个答案:

没有答案