【发布时间】:2015-03-20 14:23:27
【问题描述】:
我已经创建了我的应用程序的基本前端结构,并将其设置为部署到 Heroku。我遵循了 heroku 入门(使用 node.js)教程,一切似乎都运行良好,因为我可以看到它在我的 localhost 和在线上运行。我还使用 express 驱动程序成功安装了 MongoDB,并从位于根目录的 index.js 文件连接到它。我可以从我的 git bash 终端的控制台日志中看到此连接成功。
我的主干应用程序位于根目录上名为“Public”的文件夹中,Grunt 将其压缩为 1 个文件,该文件放置在根目录下名为“lib”的文件夹中。
我想知道如何从主干视图中访问我的数据库?
我遵循了这个例子:Populate Backbone Marionette Views with data from Mongoose using Express Route 但我没有看到与 index.js 和主干视图的联系以及它们如何交互。我也对 /cats json 文件的位置感到困惑?
我的文件夹结构是这样的:
public
--css
--fonts
--img
--js
--lib
--scss
lib
--minified-javascript-app.js
index.js
Gruntfile.js
我的 index.js 文件如下所示:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log("mongoose: connected");
});
var myDbSchema = mongoose.Schema({
name: String
});
myDbSchema.methods.logName = function () {
var name = this.name
? "The name of the site is " + this.name
: "This one don't have a name"
console.log(name);
}
Cats = mongoose.model('Cats', myDbSchema);
var dublinCat = new Cats({ name: 'Catty1' });
console.log(dublinCat.name);
var londonCat = new Cats({ name: 'Catty1' });
londonCat.logName();
londonCat.save(function (err, cats) {
if (err) return console.error(err);
console.log("All sites: ",cats);
});
//end of mongoos db stuff
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
response.send('Hello World!');
});
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});
【问题讨论】:
标签: node.js mongodb backbone.js gruntjs