2015-12-29 29 views
0

我在写一个Node.JS应用程序时遇到了问题。 我正在尝试获取JSON字符串作为POST并将该JSON保存为mongodb。404错误|时没有设置响应Node.JS&MongoDB

插入工作正常,但我无法处理来自负责与mongodb进行交互的片段的错误场景。

E.g.在下面的代码片段中,当满足各自的条件时,我会在server.js中的“错误”注释#1,3,4中得到预期的404响应,但是在注释#2中的404响应不会到来。

但是,我在控制台上从insertintomongo.js在注释#5和6中得到错误,并且该错误对象也成功发送回server.js。

即使当我知道代码已进入正确如果条件(如控制台输出所示),我无法弄清楚为什么响应未设置为404。

JS的片段处理请求(server.js

var imongo=require("./insertintomongo.js"); 

http.createServer(function (request, response) { 
if (uri == "/ssd/add" && request.method == 'POST') { 
    console.log(request.method + " to " + request.url); 
    var fullBody = ''; 

    request.on('data', function(chunk) { 
     fullBody += chunk.toString(); 
    }); 

    request.on('end', function() { 
     try { 
     JSON.parse(fullBody); 
     } catch (e) { 
     // Error 1 
     console.log('Invalid JSON. Error message: ' +e); 
     response.writeHead(404, "Input data is not in JSON format" , {'Content-Type': 'text/html'}); 
     response.end('<html><head><title>Input data is not in JSON format</title></head><body><h1>Input data is not in JSON format</h1></body></html>'); 
     } 
     var inputJSON = JSON.parse(fullBody); 
     if (inputJSON.deployment_id != undefined){ 
     inputJSON._id=inputJSON.deployment_id; 
     imongo(inputJSON,function(err){ 
      if(err){ 
      // Error 2 
      console.log('Error 2: ' + err); 
      response.writeHead(404, "Error saving input data" , {'Content-Type': 'text/html'}); 
      response.end('Error inside callback'); 
      } else { 
      console.log("All good"); 
      }; 
     }); 
     } else { 
     // Error 3 
     console.log("Incorrect json provided"); 
     response.writeHead(404, "Incorrect json provided", {'Content-Type': 'text/html'}); 
     response.end('<html><head><title>Incorrect json provided</title></head><body><h1>Incorrect json provided.</h1><h3>"deployment_id" field is missing.</h3></body></html>'); 
     }; 

     // OK 1 
     response.writeHead(200, "OK", {'Content-Type': 'text/html'}); 
     response.end(); 
    }); 
} else { 
    // Error 4 
    response.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); 
    response.end('<html><head><title>Method not supported</title></head><body><h1>Method not supported.</h1></body></html>'); 
    } 
}).listen(8081); 

insertintomongo.js它包括在上述JS

var mongodb = require('mongodb'); 

var MongoClient = mongodb.MongoClient; 
var url = 'mongodb://localhost:27017/mytest'; 
function saveToMongo(input,cb){ 
    MongoClient.connect(url, function (err, db) { 
    if (err) { 
     // Error 5 
     console.log('Unable to connect to the mongoDB server. Error:', err); 
     cb(err); 
    } else { 
     var collection = db.collection('users'); 

     collection.insert([input], function (err, result) { 
     if (err) { 
      // Error 6 
      cb(err); 
     } else { 
      console.log('Inserted '+ result.result.ok+ ' documents into collection. The documents inserted with "_id" are:' + JSON.stringify(result.ops)); 
      cb(null); 
     } 
     db.close(); 
    }); 
    } 
    }); 
}; 

module.exports=saveToMongo; 

这里的控制台输出

Server runing at 8081 
POST to /ssd/add 
// This is coming from inserttomongo.js 
Error 5: Unable to connect to the mongoDB server. Error: { [MongoError: connect ECONNREFUSED 127.0.0.1:27018] 
    name: 'MongoError', 
    message: 'connect ECONNREFUSED 127.0.0.1:27018' } 
// This is coming from server.js 
Error 2: MongoError: connect ECONNREFUSED 127.0.0.1:27018 

回答

0

好的,figu消除这个问题。我不得不从以前的地方删除这些行,并将其放在其他条件的“错误2”评论。

// OK 1 
    response.writeHead(200, "OK", {'Content-Type': 'text/html'}); 
    response.end(); 

这是因为调用imongo是异步甚至imongo尚未结束之前响应被设置200。

非常基本的错误:(