我想创建一个简单的节点博客,但我需要一些帮助!
我想将我的博客'条目'或'帖子'保存到rethinkdb,但要做到这一点我首先需要知道如何让我的帖子显示在我的博客上,所以我创建了一个rethinkdb服务器,带有一些条目!这就是它的样子:
[
{
"body": "This is the standard hello world message!" ,
"id": "1" ,
"published": "2014-04-19" ,
"title": "Hello World!"
} ,
{
"body": "I tought dinner with my family would be horrific, but maybe my low expectations made it bearable" ,
"id": "3" ,
"published": "2014-04-19" ,
"title": "Dinner was ok!"
} ,
{
"body": "Here I am ranting about how easter is not as awesome it is said to be" ,
"id": "2" ,
"published": "2014-04-19" ,
"title": "Easter is hard work"
}
]
数据库名称为'blogdb',表名为'entries'
这是我的app.js(node.js服务器)
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res) {
res.render('index',
{title: 'My Blog'}
)
});
app.get('/about', function(req, res) {
res.render('about',
{ title : 'About' }
)
})
app.get('/photos', function(req, res) {
res.render('photos',
{ title : 'Photos' }
)
})
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
最后,我们有index.jade(网页)
extend layout
block content
p this is where I want my entreis to be!
footer
p
a(href='/') Home
| ~
a(href='/about') About Me
| ~
a(href='/photos') Some photos
我知道这并不多,但在我有参赛作品之前,我不想做太多的设计!
如何将rethinkdb服务器中的所有条目都放入我的网页?
所有帮助表示赞赏!
答案 0 :(得分:5)
我个人使用sailsjs进行节点开发,所以我对具体表达并不了解......但......
你需要为初学者提供rethinkdb适配器......
var r = require('rethinkdb');
那么也许你可以把你的主页路线改成这样的......
app.get('/', function(req, res) {
r.connect({host: 'rethinkdb_server_ip', db: 'blogdb'}, function(err, conn){
r.table('entries').run(conn, function(err, cursor){
if (err) {
//Handle errors if you want.
}
cursor.toArray(function(entries){
res.render('index', {entries: entries, title: 'My Blog'})
});
});
});
});