0

所以是时候让我的 node.js/express 应用程序上线了。它在我的本地开发环境中运行良好。我是 node.js 和 mongoDB 的新手。我决定让我们 nodejitsu 作为主持人。

我需要做的第一件事是在 nodejitsu 创建数据库。然后我必须配置我的代码,以便它连接到创建的数据库。这应该如何配置?在我的本地开发环境中,对象只是像种子一样存储,而不是实际上是数据库。

我觉得我在这里遗漏了一些关于如何启动和运行我的应用程序的主要部分。如果有人能帮我整理一下,我将不胜感激。

这是本地工作的代码:

var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('spotdb', server);

db.open(function(err, db) {
    if(!err) {
        console.log("failed to connect to 'spotdb' database");
        db.collection('spots', {strict:true}, function(err, collection) {
            if (err) {
                console.log("The 'spots' collection dont exist. Creating it with dbseed");
                dbSeed();
            }
        });
    }
});


var dbSeed = function() {

    var spots = [
    {
        name: "A name",
        description: "Description",
        picture: "picture.jpg"
    },
    {
        name: "Number two",
        description: "Descp",
        picture: "image.jpg"
    }];

    db.collection('spots', function(err, collection) {
        collection.insert(spots, {safe:true}, function(err, result) {});
    });

};
4

0 回答 0