在请求执行mongodb查询的url时接收502

时间:2015-10-18 17:31:25

标签: node.js mongodb express

请求需要很长时间,最终因502错误而失败。

您可以看到app.get('/players'被调用,但db查询从不打印结果。

collections db ..

中的

lastmanstanding

> use lastmanstanding
switched to db lastmanstanding
> show collections
openshift
players
system.indexes
system.users

console.log ..

Listening on 127.2.202.129:8080
Connected correctly to server
/players requested

server.js

var express = require('express'),
    app = express(),
    ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1",
    port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    mongoDbUrl = process.env.OPENSHIFT_MONGODB_DB_URL || 27017,
    MongoClient = require('mongodb').MongoClient,
    assert = require('assert'),
    ObjectId = require('mongodb').ObjectID,
    url = mongoDbUrl + 'lastmanstanding';

MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    console.log("Connected correctly to server");

    db.close();
});

// home requested
app.get('/', function(req, res) {
    res.sendfile('index.html');
});

// players requested
app.get('/players', function(req, res) {
    console.log('/players requested');
    var findPlayers = function(db, callback) {
       var cursor = db.collection('players').find();
       cursor.each(function(err, doc) {
          assert.equal(err, null);
          if (doc != null) {
             console.log(doc);
             res.send(doc);
          } else {
             callback();
          }
       });
    };
});

// start server
app.listen(port, ipaddress, function () {
    console.log('Listening on ' + ipaddress + ':' + port);
});

1 个答案:

答案 0 :(得分:0)

嗯,您已经定义了调用mongodb但在收到请求时没有调用该函数的函数。当您收到要发送的响应请求时,需要调用findPlayers函数