ExpressJS&猫鼬表现问题

时间:2016-03-07 15:02:39

标签: node.js mongodb express mongoose openshift

来自" db"的结果和" mongo"我得等2-3分钟。

/ db 的响应应为空。 但是 / mongo 应该找到一个项目,但确实如此。

[
    {
        "created_at":"2016-03-07T12:03:06.797Z",
        "updated_at":"2016-03-07T12:03:06.797Z",
        "name":"Chris-dude",
        "username":"sevilayha",
        "password":"password",
        "_id":"56dd6dfaf12045d5621260fe",
        "__v":0
    }
]

来自 / db

的回应
  

代理错误

     

代理服务器收到来自上游服务器的无效响应。   代理服务器无法处理请求GET / db。

     

原因:从远程服务器读取错误

     

端口80的Apache / 2.2.15(Red Hat)服务器

在/ db中我想只保存项目,如果我的MongoDB中不存在。

// server.js

var express = require('express');
var app = express();

var mongoose= require('mongoose');
var User = require('./app/models/user');


//logs      
var logs = {};
function log(key, value){
    logs[key]=value;
}

//app configuration
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

//mongodb configuration
var mongoHost = process.env.OPENSHIFT_MONGODB_DB_HOST || 'localhost';
var mongoPort = process.env.OPENSHIFT_MONGODB_DB_PORT || 27017;
var mongoUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME; //mongodb username
var mongoPass = process.env.OPENSHIFT_MONGODB_DB_PASSWORD; //mongodb password
var mongoDb   = process.env.OPENSHIFT_APP_NAME; //mongodb database name

//connection strings
var mongoString = 'mongodb://' + mongoUser + ':' + mongoPass + '@' + mongoHost + ':' + mongoPort + '/' + mongoDb;


mongoose.connect(mongoString, function(err) {
    if (err) {
        log('createConnection err',err);
    }
});


var chris = new User({
  name: 'Chris',
  username: 'sevilayha',
  password: 'password' 
});

log('chris',chris);

chris.dudify(function(err, name) {
  if (err) throw err;
  log('Your new name is ' + name);
});


app.get('/db', function(req, res) {
    User.find({ username: 'sevilayha' }, function(err, user) {
       if (err) throw err;  
       if(user.length == 0){
            chris.save(function(err) {
                if (err) {
                    log('User saved successfully!',err);   
                    throw err;    
                }
            });
       } else {
      // object of the user
        log('search result',user);
           res.status(200).send(user);
           res.end();
       }
    });  
});

app.get('/logs', function(req, res) {
  res.status(200).send(logs);
});


// app is running!
app.get('/', function(req, res) { 
  res.status(200).send('OK');
});


app.get('/mongo', function(req, res) {
  User.find({ username: 'sevilayha'}, function(err, data){
    if (err) {
        log('User.find err',err);
      res.status(500).send('NOT OK' + JSON.stringify(err));
    } else {
        log('user.find data',data);
        res.status(200).send(JSON.stringify(data));
        res.end();
    }
  });
});

app.listen(port, ipaddr);

//应用/模型/ user.js的

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
    age: Number,
    website: String
  },
  created_at: Date,
  updated_at: Date
});

userSchema.methods.dudify = function() {
  this.name = this.name + '-dude'; 
};

// on every save, add the date
userSchema.pre('save', function(next) {

  var currentDate = new Date(); 
  this.updated_at = currentDate;
  if (!this.created_at)    this.created_at = currentDate;
  next();
});


var User = mongoose.model('User', userSchema);

module.exports = User;

我做错了什么?

编辑

添加res.end();

2 个答案:

答案 0 :(得分:1)

这里的问题是你的Error: ffmpeg exited with code 1: Error opening filters!路由没有让请求知道它已经完成处理,所以来自Apache的请求超时。在数据库操作完成时,您应该在该路由中添加/db

答案 1 :(得分:0)

我有结果:我将chris声明为局部变量,因此在/db内调用chris.save()会导致错误。不幸的是,这个错误不在日志中。 我用/newTom检查了它 - 它始终有效。

   var chris = new User({ //local variable!
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
    });

    chris.dudify(function(err, name) {
      if (err) throw err;
      log('Your new name is ' + name);
    });

    tom = new User({  //global
      name: 'Tom',
      username: 'Jonson',
      password: 'admin1' 
    });

    tom.dudify(function(err, name) {
      if (err) throw err;
      log('Your new name is ' + name);
    });

app.get('/db', function(req, res) {



    User.find({ username: 'sevilay' }, function(err, user) {
       if (err) throw err;  
       if(user.length == 0){
            chris.save(function(err) {
                if (err) {
                    log('User saved successfully!',err);   
                    throw err;    
                }
            });
       } else {
      // object of the user
        log('search result',user);
           res.status(200).send(user);
           res.end();
       }
    });  
});

app.get('/newTom', function(req, res) {

    User.find({ username: 'Jonson' }, function(err, user) {
       if (err) throw err;  
       if(user.length == 0){
            tom.save(function(err) {
                if (err) {
                    log('User saved successfully!',err);   
                    throw err;    
                }
            });
       } else {
        log('search result',user);
           res.status(200).send(user);
           res.end();
       }
    });  
});